I have a script that I am calling an external command with multiple times.
I first check the output folder I am passing in to the external command and if the folder exists I would like to prompt the user with:
Write-Warning "Do you want to override $somefolder" -WarningAction Inquire
This works great an displays the following to the user:
Confirm
Continue with this operation?
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"):
See documentation here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.1
In the above Y continues, H halts, and S does this odd suspend thing where the user can drop out to shell to run a few command and type exit to resume the script. Odd but neat.
My problem is if the user responds with "A" (for Yes to All) they continue being prompted each time. So obviously this is not the behavior the user would expect.
How do we handle the "A" case correctly so that if the user chooses "A" they are no longer prompted for in my script.
Here is a snippet that demonstrates the issue:
for ($i = 0; $i -lt 10; $i++) {
Write-Warning "Do you want to override $somefolder" -WarningAction Inquire
"Pretend to do something here"
}
In the above if the user presses "A" after the first question, the next 9 prompts should be skipped. How to best handles this?
Note: Obviously there are other ways to prompt and capture input from the user in PowerShell. My question here pertains to using Write-Warning "Some Message" -WarningAction Inquire which looks like was designed to handle what I'm looking for, but I can't figure out how they intended me to use the "A" option.