Functions in PowerShell
Functions in PowerShell, and most languages, are a way to create smaller, condensed units of reusable code inside a larger piece of code. I remember back in high school learning BASIC or even PASCAL, I would have a tenancy to create little programs that read like a novel - from start to finish. However, your code should really be more like a reference manual, that has chapters of information that are regularly consulted while reading. That is what functions are for.
By using functions, I find the following benefits:
The downsides I find are:
A basic function example that i use often looks like this:
Function do-Something { # This does something awesome.
[cmdletbinding()]
Param (
[string]$vCenter,
[string]$reportOutput,
[string]$login,
[string]$password
)
$pwd = ConvertTo-SecureString $password -AsPlainText -Force;
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $login,$pwd;
& "$SomeVMwareScript" -vCenterServer $vCenter -ReportOutput "$WorkingFolder" -Credential $Cred
}
### I call the function like this
$vCenter = 'Appliance.domain.com'
$InventoryLocation = '\\Server\Some\Share'
$login = 'RO-Username'
$password = 'password'
do-Something -vCenter $vCenter -WorkingFolder $InventoryLocation -login $login -password $password
In the above example, I have a function called "do-Something" that accepts various string parameters. When I call that function, I pass variables as parameters. That function then calls another PowerShell script and passes variables as parameters to it. In the above example I am also showing one way to handle credentials. Personally I try at all costs not to have credentials handled in a plain text fashion, but in the world of Infrastructure where you are gluing disparate applications together, you just don't have a choice.