gpt4 book ai didi

powershell - 如何使用powershell检查节点是否存在而不会出现异常?

转载 作者:行者123 更新时间:2023-12-04 13:20:13 26 4
gpt4 key购买 nike

我正在尝试检查特定节点是否存在,如下所示。

在我的配置文件中有一个名为 client 的节点,它可能可用也可能不可用。

如果它不可用,我必须添加它。

    $xmldata = [xml](Get-Content $webConfig)    

$xpath="//configuration/system.serviceModel"
$FullSearchStr= Select-XML -XML $xmldata -XPath $xpath

If ( $FullSearchStr -ne $null) {

#Add client node
$client = $xmldata.CreateElement('Client')
$client.set_InnerXML("$ClientNode")
$xmldata.configuration."system.serviceModel".AppendChild($client)
$xmldata.Save($webConfig)

}

我正在检查的条件可能会返回数组。

我想检查客户端节点之前是否可用?

最佳答案

您可以尝试 SelectSingleNode 方法:

$client = $xmldata.SelectSingleNode('//configuration/system.serviceModel/Client')

if(-not $client)
{
$client = $xmldata.CreateElement('Client')
...
}

关于powershell - 如何使用powershell检查节点是否存在而不会出现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13031110/

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