2016-11-30 71 views
1

我想寫簡單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它應該打破。

任何想法?

+0

也'附加內容d:\腳本\ expiringCerts \ request.inf「'r'n_continue_ =「$ dnsipname」「'不寫入文件。 – user5711825

回答

5

在PowerShell中你已經基本上三個選項提示用戶是/否的選擇。

  • Read-Host的cmdlet:

    $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?' 
    
    $yes = New-Object Management.Automation.Host.ChoiceDescription '&Yes' 
    $no = New-Object Management.Automation.Host.ChoiceDescription '&No' 
    $options = [Management.Automation.Host.ChoiceDescription[]]($yes, $no) 
    $default = 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) 
    
+0

不錯的總結。但是 - 我個人不喜歡用戶提示。在這種情況下,我會傳遞一個'string []'給包含消費者想要添加的所有DNS/IP的腳本。沒有必要提示(在我的世界);-) –

+0

謝謝! :)它幫助了我很多理解do-until循環 – user5711825

+1

@MartinBrandl提示具有它們的用途,但我必須同意大多數時候我更喜歡能夠將所需信息作爲參數傳遞。 –

2

您需要具備在do循環內爲until語句(例如,$ strQuit)進行評估的條件,以便更新它。

if語句中包裝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"' 

if ((Read-Host "Do you want to add another DNS or IP? (Y/N)") -eq "Y") { 
    do { 
     $dnsipname = Read-Host "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"' 
     $strQuit = Read-Host "Do you want to add another DNS or IP? (Y/N)" 
    } 
    until ($strQuit -eq "N") 
} 
+0

感謝這一個工作:) – user5711825

+0

你可以標記我的答案爲接受,這有助於他人知道你的問題已被回答,也有助於未來的搜索。下面是你如何做到這一點:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –

+0

當我讀你的腳本,它會繼續做循環**直到**用戶按y或Y,但只要按y或Y,它就應該繼續。同時'$ strQuit -eq「Y」 - 或「y」'將始終返回'$ true',因此它是一個無限循環。 –

3

我覺得你不需要兩個do-until循環。我也更喜歡do-while(他正在確認yY)。

您的Add-Content上的字符串插值不起作用,因爲您使用的是單引號。我想在這裏利用格式字符串:

$yesNo = Read-Host -prompt 'Do you want to add alternative DNS names or IPs into Certificate? Y/N: ' 
if ($yesNo -eq 'y') 
{ 
    do 
    { 
     $dnsipname = read-host -prompt "Please input DNS or IP as dns=alternativeCNname or ip=ipName: " 
     Write-output "$dnsipname" 
     Add-Content -Path "D:\Scripts\expiringCerts\request.inf" -value ('`r`n_continue_ = "{0}"' -f $dnsipname) 
     $strQuit = Read-Host " do you want to add another DNS? (Y/N)"     
    } 
    while($strQuit -eq 'y') 
} 
+0

mmmmm :)謝謝,你知道爲什麼'Add-Content D:\ Scripts \ expiringCerts \ request。inf(''r'n_continue_ =「{0}」'-f $ dnsipname)'不起作用? – user5711825

+0

嘗試'Add-Content -Path「D:\ Scripts \ expiringCerts \ request.inf」-value('''r'n_continue_ =「{0}」'-f $ dnsipname)' –

+1

我會認爲'-eq「y 「'因爲默認情況下不區分大小寫,所以工作正常。無需匹配運算符。 – Matt

相關問題