2012-07-19 90 views
3

我一直在玩PS3 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 
    } 
} 

回答

3

我最終解決了這個封閉InlineScript塊中的大部分工作流邏輯。通過這種方式,工作流程的每個循環仍處於並行運行(這是我想要的),但我可以自由地在工作流中使用正常PowerShell命令,參數和變量(包括$錯誤):

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

}

1

您可以嘗試在V2(在V3沒有測試過,但我認爲它的工作原理)要做得像

Catch { 
       $errorMessage = $_   } 
} 

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 };} 
+0

我確實嘗試了你的方法,但無法使其工作。 $ _變量保持空白。我本來可能做錯了什麼。無論如何感謝您的迴應!未來我會記住這個方法。 – 2012-07-20 16:07:17

+0

至少在PS V4中這對我有效。 – Trondh 2014-04-27 14:52:36

1

在Powershell工作流程中,您無法使用$ error變量。至少以我的經驗,你必須使用以下方法:

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

$errorMessage