2

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?

2 Answers 2

2

The stop-process cmdlet supports a -passthru switch which might be useful in this circumstance

so you could assign

$processresult = Stop-Process -Name notepad -Confirm -Passthru

if ($processresult.name -eq "notepad") {echo "R.I.P. Notepad"} else {echo "Notepad lives"}

if the user choose to stop the process (i.e. enters Y) then $processresult will be a Process object. If the user chooses not to end the process (i.e. enters n) it won't be. so test the resulting object and base it on that.

A bit hacky but should work.

3
  • 1
    Alternative condition for the if: $processresult -ne $null. This will work for my particular use case. Also, you might want to temporarily set $ErrorActionPreference = 'SilentlyContinue' to keep PowerShell from barfing at the user when they pick "No". It would still be great if there's a method that works for the more general case, or if someone can verify that there isn't.
    – Iszi
    Sep 4, 2013 at 18:03
  • Scratch the bit about $ErrorActionPreference. That applies to a different part of the script I'm writing. $Notepad = Get-Process notepad; while ($Notepad -ne $null) {$Notepad = Get-Process notepad}; echo 'Notepad is finally dead!' will announce an error when Notepad is finally dead unless you change $ErrorActionPreference.
    – Iszi
    Sep 4, 2013 at 18:10
  • Interestingly, you can do without the $processresult variable entirely. if ((Stop-Process -Name notepad -Confirm -Passthru) -ne $null) {echo "R.I.P. Notepad"} else {echo "Notepad lives"}
    – Iszi
    Sep 4, 2013 at 18:34
1

Writing an extra bit of code for input handling isn't that big of a deal. For example,

$input = Read-Host "Would you like to kill notepad?"

if($input.ToLower() -eq "yes") {
    Stop-Process -Name notepad
    Write-Host "R.I.P. Notepad"
} else {
    Write-Host "Notepad lives to fight another day"
}

Would work just fine and wouldn't add a ton to your script.

3
  • It's still redundant of the functionality provided by Stop-Process -Name notepad -Confirm. I'd rather have confirmation that I can't access the user input for Stop-Process before I fall back to writing my own confirmation prompt. (Even then, for this particular use case, I personally prefer @RoryMcCune's workaround.)
    – Iszi
    Sep 4, 2013 at 18:05
  • @Iszi i guess if literally all you are going to use the input for is to confirm the Stop-Process then @RoryMcCune's answer is a bit smoother. Even if it is a bit hackety hack. With input handling you could supply a reason for killing notepad, or even supply the process to kill. Sep 4, 2013 at 18:15
  • For my particular case, I don't need the user to supply a reason or the process. I have a particular application that is not multi-user friendly. So, if someone is already logged on to the server and running the application on their session I will not be able to run it on mine. I'm trying to automate the task of identifying who's running the application and (optionally) terminating their instance. If I choose to terminate it, I want the script to run the application afterward. If I choose not to terminate, the script should exit.
    – Iszi
    Sep 4, 2013 at 18:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .