PowerShell中的catch问题

时间:2012-04-17 13:52:16

标签: powershell

我遇到了catch命令的问题。我有以下脚本我正在尝试处理:

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

Catch [System.InvalidOperationException]
{
    "Your computer is unable to contact the domain"
}

每次我跑这个,虽然我没有在catch区块中得到任何东西。以下是我从脚本中获得的错误报告:

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}

有什么想法吗?


一个有效的解决方案(感谢PK和Patrick的合作贡献):

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

Catch [System.Management.Automation.RuntimeException]
{
    "Your computer is unable to contact the domain"
}

5 个答案:

答案 0 :(得分:3)

尝试捕捉System.Management.Automation.RuntimeException而不是System.InvalidOperationException

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

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

答案 1 :(得分:2)

将“-ErrorActionPreference Stop”添加到cmdlet。

例如,

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

对于不同cmdlet处理错误的方式似乎存在一些不一致,尤其是那些像Active Directory那样的“附加”cmdlet。但是,我认为基本的想法是PowerShell catch只捕获终止错误,默认情况下,上面的例外不是。因此,通过使用-EA Stop,您将强制它成为终止错误,从而触发catch块。

以下是Ed Wilson的主题: Write PowerShell Functions That Accept Pipelined Input

答案 2 :(得分:1)

我能够让这个工作:

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

Catch
{
    "Your computer is unable to contact the domain"
}
-PassThru命令上的

Add-Computer将命令的结果返回给shell。

-ErrorAction Stop告诉PowerShell在遇到错误时停止;这会抑制您看到的错误输出。

答案 3 :(得分: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

答案 4 :(得分:0)

-Passthru放在上面可以捕获错误。