【发布时间】:2017-07-05 03:01:36
【问题描述】:
有什么办法可以抑制这个错误。而不是这个丑陋而冗长的错误,我想捕获返回码值($?)来确定成功或失败
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:\>
【问题讨论】:
标签: powershell suppress