Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching errors in Workflows

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
    }
}
like image 936
Justus Grunow Avatar asked Oct 21 '25 12:10

Justus Grunow


2 Answers

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:

workflow Get-LoggedOn {
param(
    [array]$computers,
    [System.Management.Automation.PSCredential]$Credential
    )
    ForEach -parallel ($computer in $computers) {
    InlineScript {
        Try {
            $ErrorActionPreference = "Stop"
            $response = Get-WMIObject -computername $using:computer -credential $using:Credential -query "select UserName from Win32_ComputerSystem"
        }
        Catch {
            $errorMessage = $Error[0].Exception
        }
        Finally {
            $ErrorActionPreference = "Continue"
        }
        $output = [PSCustomObject]@{
            Name = $using:computer
            User = $response.UserName
            Error = $errorMessage
        }
        $output
    }
}

}

like image 169
Justus Grunow Avatar answered Oct 23 '25 04:10

Justus Grunow


You can try to do it like in V2 ( not tested in V3 but I think it works )

Catch {
                $errorMessage = $_            }
}

the Get-Member of $_ inside the catch return:

   TypeName: System.Management.Automation.ErrorRecord

Name                  MemberType     Definition
----                  ----------     ----------
Equals                Method         bool Equals(System.Object obj)
GetHashCode           Method         int GetHashCode()
GetObjectData         Method         System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo info, 
                                     System.Runtime.Serialization.StreamingContext context)
GetType               Method         type GetType()                                        
ToString              Method         string ToString()                                     
CategoryInfo          Property       System.Management.Automation.ErrorCategoryInfo CategoryInfo {get;}
ErrorDetails          Property       System.Management.Automation.ErrorDetails ErrorDetails {get;set;} 
Exception             Property       System.Exception Exception {get;}                                 
FullyQualifiedErrorId Property       System.String FullyQualifiedErrorId {get;}                        
InvocationInfo        Property       System.Management.Automation.InvocationInfo InvocationInfo {get;} 
PipelineIterationInfo Property       System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Int32, mscorlib, 
                                     Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] 
                                     PipelineIterationInfo {get;}
TargetObject          Property       System.Object TargetObject {get;}                             
PSMessageDetails      ScriptProperty System.Object PSMessageDetails {get=& { Set-StrictMode -Version 1; 
                                     $this.Exception.InnerException.PSMessageDetails };}
like image 27
CB. Avatar answered Oct 23 '25 04:10

CB.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!