I’ve been playing around with PowerShell Workflows in PS 3.0 RC, and so far, I am in love. However, there are many limitations on the sorts of things you can and can’t use inside workflows. The one I’m hung up on currently is the $Error variable. When calling my workflow, I receive the following error:
The variable 'Error' cannot be used in a script workflow.
Does anyone know how to catch the text of an error inside a workflow, or suggestions on alternative methods of error catching if you aren’t familiar with workflows? I’ve been searching around, and can find almost no information on the specifics of workflows. Thanks!
I’m trying to do something like this:
workflow Get-LoggedOnUser{
param([array]$computers,[System.Management.Automation.PSCredential]$credential)
foreach -parallel($computer in $computers) {
$response = $null
$errorMessage = $null
If (Test-Connection -ComputerName $computer -count 1 -quiet) {
Try {
$ErrorActionPreference = "Stop"
$response = Get-WMIObject -PSCredential $credential -PSComputername $computer -query "Select UserName from Win32_ComputerSystem"
$Error
}
Catch {
$errorMessage = $Error[0].exception
}
Finally {
$errorActionPreference = "Continue"
}
}
Else {
$errorMessage = "No response"
}
$output = [PSCustomObject]@{
Name = $computer
User = $response.UserName
Error = $errorMessage
}
$output
}
}
I ended up solving this by enclosing most of the logic of my Workflow in an InlineScript block. This way, each loop of the Workflow still runs in parallel (which I wanted), but I’m free to use normal PowerShell cmdlets, parameters, and variables (including $Error) within the workflow:
}