8

我对 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"
}
4

5 回答 5

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
}
于 2012-04-17T14:26:57.203 回答
2

将“-ErrorActionPreference Stop”添加到您的 cmdlet。

例如,

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

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

这是 Ed Wilson 的主题:编写接受流水线输入的 PowerShell 函数

于 2012-04-17T15:49:35.600 回答
1

I was able to get this to work:

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

Catch
{
    "Your computer is unable to contact the domain"
}

-PassThru on the Add-Computer command returns the results of the command to the shell.

-ErrorAction Stop tells PowerShell to stop when it encounters an error; this suppresses the error output you were seeing.

于 2012-04-17T15:47:06.567 回答
0

-Passthru它放在上面可以让它捕捉到错误。

于 2012-04-17T17:05:04.880 回答
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
于 2012-04-17T15:38:29.430 回答