I am trying to write a simple
do..until
loop and it does not work:
$yesNo = Read-Host -Prompt 'Do you want to add alternative DNS names or IPs into Certificate? Y/N: '
$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)"
$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")
This one does loop twice only, but it should loop every time I hit Y
but when I hit N
or n
It should break.
Any ideas?
–
The Read-Host
cmdlet:
$msg = 'Do you want to add alternative DNS names or IPs into Certificate? [Y/N]'
$response = Read-Host -Prompt $msg
if ($response -eq 'y') {
# prompt for name/address and add to certificate
} until ($response -eq 'n')
Use -like 'y*'
and -like 'n*'
if you want to ignore trailing characters in the response.
The PromptForChoice()
method:
$title = 'Certificate Alternative Names'
$msg = 'Do you want to add alternative DNS names or IPs?'
$options = '&Yes', '&No'
$default = 1 # 0=Yes, 1=No
$response = $Host.UI.PromptForChoice($title, $msg, $options, $default)
if ($response -eq 0) {
# prompt for name/address and add to certificate
} until ($response -eq 1)
The choice
command:
$msg = 'Do you want to add alternative DNS names or IPs into Certificate'
choice /c yn /m $msg
$response = $LASTEXITCODE
if ($response -eq 0) {
# prompt for name/address and add to certificate
} until ($response -eq 1)
–
–
I think you don't need two do-until
loop. I also would prefer a do-while
(while he is confirming with y
or Y
).
Your string interpolation on the Add-Content
doesn't work because you are using single quotes. I would leverage a format string here:
$yesNo = Read-Host -prompt 'Do you want to add alternative DNS names or IPs into Certificate? Y/N: '
if ($yesNo -eq 'y')
$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')
–
–
–
You need to have the condition that you are evaluating in the until
statement ($strQuit for your example) inside the do
loop for it to be updated.
Wrapping the do
loop in an if
statement also makes the process friendlier to the user as it ask them before continuing to prompt.
$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") {
$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")
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0
with attribution required.
rev 2020.2.7.36004