How to handle the ALL case when using Write-Warning "Some Message" -WarningAction Inquire

Viewed 130

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.

1 Answers

Good question. Not too long ago MVP Kirk Munro created an issue on the PowerShell GitHub tracker about the inconsistent ActionPreference.Inquire feature. His scope is broader, I only cite the relevant section here:

There is no "Yes to All" functionality for these messages from Inquire. "Yes to All" is really "Silently Continue", except for these messages it's not silent at all. For everything except non-terminating errors, the message is displayed first. For non-terminating errors, the message is displayed after the Inquire prompt. So "Yes to All" aka "SilentlyContinue" is just broken.

Further down Bruce Pay's comment can be read as an answer to your question:

BTW - the context of these prompts is the current command/pipeline, not the script running the pipeline.

Thus, the inquire yes to all action does not work as desired.

I think there was no immediate change and the issue was only added as a future milestone.

Related