Powershell Quirks

Calling functions: Parantheses or not?

When calling powershell functions, do not use parantheses and commas:

function Add($x, $y) {
    return $x + $y
}

$result = AddNumbers (5, 10) # Wrong! This passes the array (5,10) as the first argument
$result = AddNumbers 5 10 # Right

Source & more info: http://ss64.com/ps/syntax-functions.html

However, when calling methods of .NET objects, you should provide parantheses and commas for function calls.

$now = Get-Date # Note this is a System.DateTime object
$inAYear = $now.AddYears(1)

Writing output and returning stuff from functions is (mostly) the same

You can consider functions as part of your pipe and filter mechanism. Not only what you return using the return statement is returned, but also everything output with echo or Write-Output. Here are some examples of what is and what is not passed as output of a function.

Function A() {
    'a' # passed as output
}

Function B() {
    return 'b' # passed as output
}

Function C() {
    echo 'c' # passed as output
}

Function D() {
    Write-Output 'd' # passed as output
}

Function E() {
    Write-Host 'e'
}

Function F() {
    Write-Verbose 'f'
}

$out = "
A: '{0}'
B: '{1}'
C: '{2}'
D: '{3}'
E: '{4}'
F: '{5}'
" -f (A), (B), (C), (D), (E), (F)
Write-Output $out

The result of execution is:

A: 'a'
B: 'b'
C: 'c'
D: 'd'
E: ''
F: ''

Scoping… Global? Local?

Using a global variable inside a function introduces a local copy of that variable and does not update the global var by default:

#definitions
$someglobal = 42</code>

Function IncrementAndPrint() {
    Write-Host "IncrementAndPrint before inc: $someglobal"
    $someglobal++ # Writing to a global introduces a local copy of the global var
    Write-Host "IncrementAndPrint after inc: $someglobal"
}

# Script
Write-Host "Script before: $someglobal"
IncrementAndPrint
Write-Host "Script after: $someglobal"

The output is:

Script before: 42
IncrementAndPrint before inc: 42
IncrementAndPrint after inc: 43
Script after: 42

To really a global you can change function IncrementAndPrint as follows:

Function IncrementAndPrint() {
    Write-Host "IncrementAndPrint before inc: $someglobal"
    $global:someglobal++
    Write-Host "IncrementAndPrint after inc: $someglobal"
}

The result is now:

Script before: 42
IncrementAndPrint before inc: 42
IncrementAndPrint after inc: 43
Script after: 43

Invoking external commands

A good resource on invoking external commands can be found here:
http://edgylogic.com/blog/powershell-and-external-commands-done-right/

Wednesday, October 22nd, 2014 Uncategorized

Leave a Reply

*