2012-04-17 7 views
8

Catchコマンドに問題があります。PowerShellでのキャッチに関する問題

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

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

私はキャッチに何かを得ていないのです。このかかわらを実行するたびに:私はプロセスにしようとしている次のスクリプトを持っています。ここで私がスクリプトから得るエラーが報告されています:

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とパトリックのおかげで)

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

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

答えて

3

ではなくSystem.InvalidOperationExceptionSystem.Management.Automation.RuntimeExceptionをキャッチしてみます。

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

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

応答をありがとう、私はSystem.Management.Automation.RuntimeExceptionに置き換えましたが、私はまだ同じ問題があります。私はエラーを取り除き、キャッチだけを使ってみましたが、問題は解決しません。 –

+0

キャッチブロックにはまったく入ることはできませんか?これを試してみて、出力が何であるかを教えてください:試してみてください { \tスロー } キャッチ { \t $ _ Exception.GetType()|。 FullName を選択してください} –

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

私は仕事にこれを取得することができた:Add-Computerコマンドの

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

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

-PassThruをシェルにコマンドの結果を返します。

-ErrorAction Stopは、エラーが発生したときにPowerShellを停止するよう指示します。これはあなたが見ていたエラー出力を抑制します。

2

"-ErrorActionPreference Stop"をCmdLetに追加します。例えば

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

は特には「アドオン」は、異なるコマンドレットのプロセスエラー、Active Directoryのもののようなコマンドレットの方法でいくつかの矛盾があるように思えるん。しかし、私は基本的な考えは、Powershell Catchは終了エラーを捕捉するだけだと考えています。上記の例外はデフォルトではありません。したがって、-EA Stopを使用すると、強制的に終了エラーとなり、Catchブロックがトリガーされます。ここで

はエド・ウィルソンは、被写体にあります:http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx

0

パトリック:トリックをしたおかげで、。 -Passthruを置くことで、エラーをキャッチすることができました。元の投稿を回答で更新しました。

関連する問題