如何使用powershell检查节点是否存在而不会出现异常? [英] How to check whether a node exists or not using powershell without getting exception?
本文介绍了如何使用powershell检查节点是否存在而不会出现异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检查特定节点是否存在,如下所示.
I am trying to check whether a particular node exists or not like follows.
在我的配置文件中有一个名为 client 的节点,它可能可用也可能不可用.
In my config file there is a node named client ,it may or may not available.
如果它不可用,我必须添加它.
If it is not available i have to add it.
$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)
}
我正在检查的条件可能会返回数组.
The condition i am checking may return array.
我想检查客户端节点之前是否可用?
i would like to check whether the client node available before or not?
推荐答案
为什么你不能这样做:
$xmldata = [xml](Get-Content $webConfig)
$FullSearchStr = $xmldata.configuration.'system.serviceModel'
这篇关于如何使用powershell检查节点是否存在而不会出现异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文