gpt4 book ai didi

powershell - 直到用户输入是/否

转载 作者:行者123 更新时间:2023-12-04 00:04:23 27 4
gpt4 key购买 nike

我正在尝试写一个简单的 do..until循环,它不起作用:

$yesNo = Read-Host -Prompt 'Do you want to add alternative DNS names or IPs into Certificate? Y/N: '

do {
$dnsipname = Read-Host -Prompt "Please input DNS or IP as dns=alternativeCNname or ip=ipName: "
Write-Output "$dnsipname"
$strQuit = Read-Host " do you want to add another DNS? (Y/N)"
do {
$dnsipname = Read-Host -Prompt "Please input DNS or IP as dns=alternativeCNname or ip=ipName: "
Write-Output "$dnsipname"
Add-Content D:\Scripts\expiringCerts\request.inf '`r`n_continue_ = "$dnsipname"'
} until ($strQuit -eq "Y" -or "y")
} until ($yesNo -eq "Y" -or "y")

这个只循环两次,但每次我点击 Y 时它都应该循环。但是当我点击 Nn它应该打破。

有任何想法吗?

最佳答案

在 PowerShell 中,您基本上有三个选项来提示用户选择是/否。

  • Read-Host 小命令:
    $msg = 'Do you want to add alternative DNS names or IPs into Certificate? [Y/N]'
    do {
    $response = Read-Host -Prompt $msg
    if ($response -eq 'y') {
    # prompt for name/address and add to certificate
    }
    } until ($response -eq 'n')

    使用 -like 'y*'-like 'n*'如果您想忽略响应中的尾随字符。
  • PromptForChoice() 方法:
    $title   = 'Certificate Alternative Names'
    $msg = 'Do you want to add alternative DNS names or IPs?'
    $options = '&Yes', '&No'
    $default = 1 # 0=Yes, 1=No

    do {
    $response = $Host.UI.PromptForChoice($title, $msg, $options, $default)
    if ($response -eq 0) {
    # prompt for name/address and add to certificate
    }
    } until ($response -eq 1)
  • choice 命令:
    $msg = 'Do you want to add alternative DNS names or IPs into Certificate'
    do {
    choice /c yn /m $msg
    $response = $LASTEXITCODE
    if ($response -eq 0) {
    # prompt for name/address and add to certificate
    }
    } until ($response -eq 1)
  • 关于powershell - 直到用户输入是/否,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40889444/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com