Prof. Powershell

Keeping Your Options Open

Now that you're familiar with functions, here's how to pass information to them. A look at the Param keyword.

Last time I introduced you to PowerShell functions and suggested ways to develop them so they could be valuable PowerShell tools. Here's what a simple function looks like:

Function Do-Something {
#run some code here
}

Once the function is defined you can call it by simply using the function name:

PS C:\> do-something

But often you will need to pass parameters or run-time arguments to your function. You'll likely see two different approaches. This is the way I prefer:


Function Do-Something {
Param([objecttype]$variable=default)
#run some code here
}

The Param keyword defines the parameter set, which is a comma-separated list of variables. I recommend the best practice of casting your parameter variable of a certain type and providing a default value. By specifying a type, you can catch errors right away if someone uses the function incorrectly. Providing a default value also is helpful if the majority of the time you want to use the same value anyway, but want to preserve your options to specify something else. If you don't specify a default value, then you should include some sort of validation code to make sure the right type of information was passed. Here's a more complete example:

Function Check-Service {
Param([string]$computername=$env:computername,
[string]$service="spooler" )

$wmi=get-wmiobject win32_service -filter "name='$service'" -computername $computername
if ($wmi.state -eq "running") {
write $True
}
else {
write $False
}
}

This function takes a computer name and a service name as parameters and returns $True or $False depending on whether the service is running. Because the Get-WMIObject cmdlet needs strings for the filter and computername, I make sure the parameters are also strings. I've also set default values of the local computer name and the spooler service. So if I run the function without any parameters that is what will be checked. But I can specify other computers and services:

PS C:\ Check-Service Print01 Browser

The parameters are positional, meaning the function is expecting a computername and then a service name. But I can also use the parameter names like this

PS C:\ Check-Service -computername Print01 -service Browser

This is especially helpful on more complex functions because I can mix the parameters in any order, or if I want to use some of the defaults:

PS C:\ Check-Service -service Browser

This will check the Browser service on the local machine. Stay tuned -- there's more fun with functions to come.

About the Author

Jeffery Hicks (MCSE,MCSA,MCT) is a Microsoft MVP and an IT veteran with almost 20 years of experience, much of it spent as an IT consultant specializing in Windows server technologies. He works today as an independent author, trainer and consultant. Jeff has co-authored or authored several books, courseware, and training videos on administrative scripting and automation. His latest book is Managing Active Directory with Windows PowerShell: TFM (SAPIEN Press 2008). You can follow Jeff at twitter.com/jeffhicks and jdhitsolutions.com/blog.

Reader Comments:

Wed, Mar 4, 2009 Anonymous Anonymous

Concise and complete with examples.

Wed, Mar 4, 2009 Anonymous Anonymous

Very good wok

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above