3

我一直在玩 PS 3.0 RC 中的 PowerShell 工作流,到目前为止,我很喜欢。但是,您可以在工作流程中使用和不能使用的东西有很多限制。我目前挂断的一个是 $Error 变量。调用我的工作流程时,我收到以下错误:

The variable 'Error' cannot be used in a script workflow.

有谁知道如何在工作流程中捕获错误文本,或者如果您不熟悉工作流程,有关于错误捕获替代方法的建议吗?我一直在四处寻找,几乎找不到有关工作流程细节的信息。谢谢!

我正在尝试做这样的事情:

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
    }
}
4

3 回答 3

5

我最终通过将工作流的大部分逻辑封装在一个 InlineScript 块中来解决这个问题。这样,工作流的每个循环仍然并行运行(这是我想要的),但我可以在工作流中自由使用普通的 PowerShell cmdlet、参数和变量(包括 $Error):

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
    }
}

}

于 2012-07-20T16:06:10.913 回答
1

您可以尝试像在 V2 中那样做(未在 V3 中测试,但我认为它有效)

Catch {
                $errorMessage = $_            }
}

在catch 返回Get-Member$_

   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 };}
于 2012-07-20T07:02:33.197 回答
1

在 Powershell 工作流中,您无法使用 $error 变量。至少根据我的经验,您必须使用以下方法:

Try {
     Throw "This is an error exception."
}
Catch {
     $errorMessage = $_
}

$errorMessage
于 2014-06-27T13:16:13.710 回答