2012-04-17 27 views
8

Có vấn đề với lệnh Catch. Tôi có tập lệnh sau đây tôi đang cố gắng xử lý:Các vấn đề với Catch in PowerShell

Try 
    { 
    Add-Computer -DomainName "MyDomain.Dom" -Credential $DomainCred -PassThru -ErrorAction Stop 
    } 

Catch [System.InvalidOperationException] 
    { 
    "Your Computer Is Unable To Contact The Domain" 
    } 

Mỗi khi tôi chạy điều này mặc dù tôi không nhận được bất kỳ thứ gì trong Catch. Dưới đây là báo cáo lỗi mà tôi nhận được từ các kịch bản:

PSMessageDetails  : 
Exception    : System.InvalidOperationException: This command cannot be executed on target computer('') due to following error: The specified domain either does not exist or could not 
         be contacted. 
TargetObject   : 
CategoryInfo   : InvalidOperation: (MYPC:String) [Add-Computer], InvalidOperationException 
FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.AddComputerCommand 
ErrorDetails   : 
InvocationInfo  : System.Management.Automation.InvocationInfo 
PipelineIterationInfo : {0, 1} 

Bất kỳ ý tưởng?

Giải pháp công tác (Nhờ PK và Patrick vì những đóng góp của họ kết hợp)

Try 
    { 
    Add-Computer -DomainName "MyDomain.Dom" -Credential $DomainCred -PassThru -ErrorAction Stop 
    } 

Catch [System.Management.Automation.RuntimeException] 
    { 
    "Your Computer Is Unable To Contact The Domain" 
    } 

Trả lời

3

Hãy thử bắt System.Management.Automation.RuntimeException thay vì System.InvalidOperationException.

Try 
{ 
    Add-Computer -DomainName "MyDomain.Dom" -Credential $DomainCred 
} 

Catch [System.Management.Automation.RuntimeException] 
{ 
    'Error: {0}' -f $_.Exception.Message 
} 
+0

Cảm ơn bạn đã phản hồi, tôi đã thay thế bằng System.Management.Automation.RuntimeException nhưng tôi vẫn gặp sự cố tương tự. Tôi thậm chí đã cố gắng đưa ra bất kỳ lỗi và chỉ sử dụng Catch nhưng vấn đề vẫn tồn tại. –

+0

Bạn không thể vào được khối catch? Hãy thử điều này và cho tôi biết những gì mà đầu ra là: Cố gắng { \t ném } Catch { \t $ _ Exception.GetType() |. chọn Họ và tên } –

0
FullName                                         
--------                                         
System.Management.Automation.RuntimeException                                
The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a user-specified "f 
ormat-list" command which is conflicting with the default formatting. 
    + CategoryInfo   : InvalidData: (:) [out-lineoutput], InvalidOperationException 
    + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand 
1

tôi đã có thể có được điều này để làm việc:

Try 
    { 
    Add-Computer -DomainName "MyDomain.Dom" -Credential $DomainCred -PassThru -ErrorAction Stop 
    } 

Catch 
    { 
    "Your Computer Is Unable To Contact The Domain" 
    } 

-PassThru trên lệnh Add-Computer trả về kết quả của các lệnh để vỏ.

-ErrorAction Stop yêu cầu PowerShell dừng khi gặp lỗi; điều này ngăn chặn đầu ra lỗi bạn đang thấy.

2

Thêm "-Không phục hồi hành độngTruy cập dừng" vào CmdLet của bạn.

Ví dụ,

Add-Computer -DomainName "MyDomain.Dom" -Credential $DomainCred -EA Stop 

Có vẻ như là một vài mâu thuẫn với những cách mà cmdlets khác nhau quá trình lỗi, đặc biệt là những "add-on" cmdlets giống như những cái Active Directory. Tuy nhiên, tôi nghĩ rằng ý tưởng cơ bản là Powershell Catch chỉ bắt lỗi chấm dứt, trong đó ngoại lệ của bạn ở trên không phải là theo mặc định. Vì vậy, bằng cách sử dụng -EA Dừng bạn buộc nó là một lỗi chấm dứt, mà kích hoạt khối Catch.

Đây là Ed Wilson về chủ đề: http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx

0

Patrick: Xin cảm ơn, điều đó đã xảy ra. Đặt -Passthru trên nó cho phép nó để Catch lỗi. Tôi đã cập nhật bài đăng gốc bằng câu trả lời.

Các vấn đề liên quan