Powershell Functions I

Call me crazy, but don’t think the whole method of declaring and calling function parameters is particularly intuitive in PowerShell. But let’s go back to the notion of the function.  What is a function (in PowerShell)?

A function is a block of code that takes variables as input, manipulates those variables and returns one more variables as a result.  In PowerShell, a function can exist independently as a cmdlet (either in its own standing .ps1 file or as part of a .pm1 module file.) or as a block of code in a larger script.  If the function is used within a larger script, the function code must precede the line which calls the function. This can get unwieldy and there is a workaround; but is the price we pay for scripting, instead of programming  in C# or whatever.

function Get-ReallySimple($lname,$fname,$age){

"You passed in the following: $lname, $fname, $age"

}

PS>Get-MyParams Joe Dokes 32   

returns

You passed in the following: Joe, Dokes, 32

Note that this is a single string displaying the three variables.

I think that’s about the simplest function you  can write. It takes (up to) three parameters, and returns them as part of a single string which is output to the pipeline.

Functions return their results to the pipeline, with or without a return key word at the end of the function.

if you try calling the function with “conventional” notation using paranthesis, PowerShell returns an error:

PS>Get-ReallySimple(joe,dokes,32)

At line:1 char:21
+ Get-ReallySimple(joe,dokes,32)
+                     ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingArgument

If you change the code just display the variables, the resulting output is an array.

function Get-ReallySimple($lname,$fname,$age) {
  $lname
  $fname
  $age
}

This looks nice in a grid…

Get-ReallySimple joe dokes 32 | Out-GridView 

psgrid

…and shows that the output was an array with three elements, two strings and an integer.

PowerShell functions can accept input variables as part of the function call, or, optionally, from the pipeline. If a function is called with variables, they are simply added after the function call, without parentheses.

Get-MyParams Joe Dokes 32   

Not

Get-MySimpleFunction(Joe,Dokes,32)

If you type this second example, you are actually passing in an array to the function. Who knew?

Advertisements

One thought on “Powershell Functions I

  1. Pingback: PowerShell Functions II – Output function results as a hash table | Powershell Notebook

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s