Functions, Parameters and Scope PowerShell Functions, Parameters and Scope Copyright © 2016 – Curt Hill
Introduction Although we can define functions in PowerShell – there is little need for them Each file is considered a procedure No need for a header to define We do have to consider the notion of parameters as well as scope Copyright © 2016 – Curt Hill
Parameter Connection In C++ there is only one way to connect a actual and formal parameter This is the position in the parameter list void xyz(int a, float b){…} … xyz(4, z/2) The first actual (4) connects to the first formal (a) The second actual (result of z/2) connects with second formal (b) Copyright © 2016 – Curt Hill
The Other Way In several languages, such as Ada and JavaScript, named or keyword parameters are allowed As well as positional parameters In a keyword parameter the call may specify the parameter name and then the value Often positional parameters are used for the required values and keyword parameters for the defaulted ones Copyright © 2016 – Curt Hill
Parameters Recall that in the batch language there was no declaration of parameters Instead they were numbered: %1, %2 … PowerShell is different, there is a declaration This declaration is started with Param and followed with a parenthesized list of the parameters Copyright © 2016 – Curt Hill
The Parameter Set The form is: Param ( [Parameter(options)] [type] name, … [Parameter(options)] [type] name ) There may one or more parameters Several of these pieces are optional Copyright © 2016 – Curt Hill
Using Parameters Positional parameters are given following the name of the script In the same order as the parameter block They follow the script name Keyword parameters have the form: -name value Where –name is the name given in parameter block We now look at examples Copyright © 2016 – Curt Hill
Example 1 Consider the beginning of a script named scr: Param( [string]$spec, [string]$fn ) $files = get-childitem $spec This can be called by: scr *.ppt ppt_size.txt Or by: scr -fn pptsz.txt -spec *.ppt Copyright © 2016 – Curt Hill
Example 2 – Optional Type The [String] that precedes the parameter is optional Thus the parameter block could be: Param( $spec, $fn ) The line orientation is free as well: Param($spec,$fn) Multiple lines are often more readable Copyright © 2016 – Curt Hill
Example 3 - Defaults Parameters may have default values by appending an = and the value For example: Param($spec,$fn=“sizes.txt”) Now it may be called with one parameter: scr *.zip scr –spec *.zip Copyright © 2016 – Curt Hill
Example 4 - Mandatory A parameter left out becomes void Empty string We may use the options to require the parameter: Param( [Parameter(Mandatory=$True)] $spec, $fn="sizes.txt“ ) Now if called with no parameters it asks for the value of Spec Copyright © 2016 – Curt Hill
Switch Besides the string type there is a switch type for parameters By default a switch type is false Only set to true if the parameter is present Copyright © 2016 – Curt Hill
Example 5 Suppose the Script parameter: Param( [Parameter(Mandatory=$True)] $spec, $fn="sizes.txt", [switch]$announce ) … if($announce){ … If –announce is in call then $announce is true otherwise false Copyright © 2016 – Curt Hill
Example 6 – Position Normally the position of the parameters must follow the order in the parameter set This may be changed by the position option Param( $fn="sizes.txt", [Parameter( position = 1)] $spec, [switch]$announce) $spec must still be first Copyright © 2016 – Curt Hill
Functions A function may be defined using the following form: function fname { … } Where fname is the name of the function If you wish to use parameters then a param block is included Just like a script Calling a function has the same form as calling a script file Once a function is defined the name is retained for the duration of the session Copyright © 2016 – Curt Hill
Variables Again Variable is an object in regards to cmdlets The following cmdlets are available Clear-variable Get-variable New-variable Remove-variable Set-variable You should understand what each of these do Copyright © 2016 – Curt Hill
Scope You will recall scope in C++ This is not the way of PowerShell Every {} opens a new block Search current block If not found exit to enclosing block Keep going until global scope or found it This is not the way of PowerShell Copyright © 2016 – Curt Hill
Scope Levels PowerShell believes in several different scopes Global – the parent of all others Local – the current scope May be global, part of session or part of a script Script – Each script file has its own scope Copyright © 2016 – Curt Hill
Accessing A session which starts a script has the parent scope It is part of the global scope Each script that starts another script has the parent scope It is the child of a session or another script Copyright © 2016 – Curt Hill
Get-variable Several parameters of interest The first parameter can be the name of a variable Without the $ The –scope parameter shows all those variable in that scope Values include global, local Many of the variable cmdlets also allow you to specify scope Copyright © 2016 – Curt Hill
Finally This should do it for us Now how about an exercise? Copyright © 2016 – Curt Hill