functions
function Get-Double {
param([int]$n)
return $n * 2
}
Parameters are declared with param
Return is optional, the last expression is returned
Advanced functions
function Get-Data {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Host
)
$Host
}
[CmdletBinding()] makes a script behave like a built in cmdlet
Supports -Verbose, -Debug, and validation attributes
Classes
class Target {
[string]$Host
[int]$Port
Target([string]$host, [int]$port) {
$this.Host = $host
$this.Port = $port
}
}
Classes arrived in PowerShell 5 They are simpler than .NET classes but cover most scripting needs
Functions as objects
PowerShell treats functions as first class You can pass them as arguments and return them That is the basis of pipeline based tooling
Next
Modules in 07 modules
Test this in your lab first
Posted