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"
}
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. –
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 } –