$data = [xml]$-6ren">
gpt4 book ai didi

powershell - 如何在变量分配期间抑制PowerShell错误消息

转载 作者:行者123 更新时间:2023-12-02 23:41:00 66 4
gpt4 key购买 nike

有什么办法可以抑制此错误。我想捕获返回码值($?)来确定成功或失败,而不是这个丑陋而漫长的错误。

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

最佳答案

试一下命令

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

关于powershell - 如何在变量分配期间抑制PowerShell错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42266527/

66 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com