2017-02-16 11 views
1

このエラーを抑制する方法はありますか?代わりに、この醜いと長いエラーのため、私は成功したか失敗したか変数代入中にPowerShellエラーメッセージを表示されないようにする方法

PS C:\> $str ="<p> Hi </p>" 
    PS C:\> $data = [xml]$str 
    PS C:\> $? 
    True 
    PS C:\> 
    PS C:\> $str ="<p> Hi <p>" 
    PS C:\> $data = [xml] $str 
    Cannot convert value "<p> Hi <p>" to type "System.Xml.XmlDocument". Error: "Unexpected end of file has occurred. The following elements are 
    not closed: p, p. Line 1, position 11." 
    At line:1 char:1 
    + $data = [xml] $str 
    + ~~~~~~~~~~~~~~~~~~ 
     + CategoryInfo   : InvalidArgument: (:) [], RuntimeException 
     + FullyQualifiedErrorId : InvalidCastToXmlDocument 

    PS C:\> $data = [xml] $str 2> $null 
    Cannot convert value "<p> Hi <p>" to type "System.Xml.XmlDocument". Error: "Unexpected end of file has occurred. The following elements are 
    not closed: p, p. Line 1, position 11." 
    At line:1 char:1 
    + $data = [xml] $str 2> $null 
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     + CategoryInfo   : InvalidArgument: (:) [], RuntimeException 
     + FullyQualifiedErrorId : InvalidCastToXmlDocument 

    PS C:\> 
    PS C:\> $? 
    False 
    PS C:\> 

答えて

0

置きますコマンドの周りにtryキャッチ応答のための

try {$data = [xml] $str } catch {} 
+0

感謝を決定するために($?)戻りコード値をキャプチャしたいと思います。出来た。 –

関連する問題