I'm writing a script that I want to be able to react depending on the user's answer to a confirmation prompt that is part of a cmdlet being run in the script. For example:
Stop-Process -Name notepad -Confirm
if ($userresposne -eq "Yes") {echo "R.I.P. Notepad"} else {echo "Notepad lives to fight another day"}
I need to know what I should be using in place of $userresponse
to call up the user's input to the confirmation prompt in Stop-Process
. (Once this is known, I'm sure I can figure out what should properly be used in place of "Yes"
.) Is there a way to do this?
I'm working in PowerShell 3.0 but I need the script to be backward-compatible to 2.0.
The key here is that I don't want to have to write my own prompting mechanism into the script. I'm aware that there are ways to do this, and I've done so in other scripts. However, since Stop-Process
is already capable of prompting the user for the input that I need, adding those extra lines here would seem a little redundant.
In batch scripting, I'd probably be looking for something like the %ERRORLEVEL%
environment variable. Is there something similar that I could use here?