Pass Read-Host variable to Function?

by Ricky Bobby at 2012-08-26 14:15:15

So, today’s project is setting up my profile. I’ve done some basic things, a couple of which are aliases for commonly used apps, and then functions for CloseMainWindow when I want to close apps from the CLI. All great, but then I had the thought, why make new functions trying to account for every process I might want to close when I could just have it prompt me for process name? Ok, “good idea you!”, so this should be easy . . .

$closeWindow = Read-Host "which app dies today?“

function byeprocess ($closeWindow)
Get-Process $closeWindow | % { $.CloseMainWindow() }

Nope! I get prompted but nothing happens to Outlook. This works as just a function with no variable so I’m close . . . “help about_Functions” . . . google google google.

Hmmm, ok, what about:

function byeprocess
$closeWindow = Read-Host "which app dies today?"

Get-Process $closeWindow | % { $
.CloseMainWindow() }

Nope, not even prompted (no errors either).

K, what about . . .

function byeprocess ($closeWindow = Read-Host “which app dies today?”)
Get-Process $closeWindow | % { $.CloseMainWindow() }

Nah, error in ISE stating “Missing ‘)’ in function parameter list.”, no need to even try running that.

/me goes back to the googles thinking it’s parameters that I’m after . . .
by poshoholic at 2012-08-26 18:53:37
You’re forgetting something rather important. You’re not calling your function. Declaring a function is one thing (which you are doing), calling (aka invoking) a function is another (this is what you’re forgetting).

You want to take your result from Read-Host and then on a new line invoke:

[script=powershell]byeprocess $closeWindow[/script]
Also, instead of "Get-Process $closeWindow | % {$
.CloseMainWindow()}”, why not do this instead:

[script=powershell]Stop-Process -Name $closeWindow[/script]
In fact, with Stop-Process you don’t even need to create a function at all, so you can simply use Read-Host and Stop-Process and that’s it.

NOTE: spps is an alias for Stop-Process, which can be very convenient so that you don’t need to type as much and can do this in PowerShell on demand. If I want to stop a running calc process on my system, I can just invoke this in PowerShell

[script=powershell]spps -n calc[/script]
by Ricky Bobby at 2012-08-27 06:52:21
Right, duh! (and wow, haha).

As for stop-process, I want it to prompt me to save work if necessary (e.g. open email for Outlook) so that wouldn’t work it’d just kill it straight out.

Anyway, couple things I noticed are:

Being that it’s going in my profile, it prompts me straight-away. If I enter a blank value, I get an error as expected. I figured that was ok and it would just be something I enter at run-time, but . . . nope. Seems it’s broken unless I enter when asked at the start of PS.
Also, If I do specify a process, it’s going to save that value, no? So really I’m stuck with what ever I entered it initially anyway?
by Ricky Bobby at 2012-08-27 06:54:58
P.s. Going to think about this more, but just woke up a bit ago and discovered my moto has been stolen, so my mind is elsewhere. Grrrrr.
by poshoholic at 2012-08-27 12:55:20
What you really want is to simply declare the function in your profile and make the parameter mandatory. Then when you call it later, it will prompt you for the process name if you didn’t give it one.

[script=powershell]function Stop-MyProcess {
Stop-Process -Name $Name

Once you have that declared, then if you invoke Stop-MyProcess it asks you for a name. That’s how you tie the prompting into the actual function. And inside the function you can make it stop the process however you like and also include whatever you want (like special cases for Outlook if you like).
by poshoholic at 2012-08-27 12:55:50
P.S. Sorry to hear about your moto being stolen. :frowning: