Error Handling
there is a $error variable that has a default limit of 256 (defined by $MaximumErrorCount) the most recent error will always be $error[0] $error
$error[0]
$error.count
you can use –errorvariable on most cmdlets to define your own error variable Get-WmiObject -Class win32_ComputerSystem -ErrorVariable $WMIError
$? #Was the last command successful true/false
$LastExitCode #error code returned by external command or script (ping blah) Try Catch Finally$foo = {} | Select Name, Ram, Tier
$foo.Name = "test"
$foo.Ram = 128
$foo.Tier = 0
$blah = $foo | select Name, @{Name="ratio";Expression=
{
try {
($_.Ram / $_.Tier)
}
catch [System.DivideByZeroException] {
"Please don’t divide by 0!"
}
finally {
# Cleanup
}
}}
$blah
|
|