2017-02-16 3 views
1

Gibt es eine Möglichkeit, diesen Fehler zu unterdrücken. Statt diesen hässlichen und langen Fehler, würde Ich mag Rückgabewert erfassen ($?) Erfolg oder Misserfolg um den BefehlSo unterdrücken Sie die PowerShell-Fehlermeldung während der Variablenzuweisung

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:\> 

Antwort

0

Platz einen Versuch fängt

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

Vielen Dank für die Antwort zu bestimmen. Es funktionierte. –

Verwandte Themen