我有XML插入本地主机名。主机名可以是以下任何一种:
<name>lonmq1111</name>
<name>stoms1111</name>
<name>bqqlk1111</name>
<name>hkgtp1111</name>
根据主机的名称,下面的脚本需要将正确的网关添加到XML。例如:
IF <name>lon*<name> OR <name>sto*<name> THEN
add these gateways
ELSEIF <name>bqq*</name> OR <name>hkg*</name> THEN
add different gateways
ELSEIF etc.
我有以下内容,但它不起作用。关于如何在中间使用通配符的任何想法?
$file = Get-Content C:\testnew.xml
if ($file -like ' <name>lon*</name>' -or '<name>sto*</name>') {
# load XML file
[xml]$doc = Get-Content "C:\testnew.xml"
# create node <hostname>
$comp = $doc.CreateNode('element', 'hostname', '')
$desc = $doc.CreateTextNode('test')
$comp.AppendChild($desc)
# create node <port>
$sref = $doc.CreateNode('element', 'port', '')
$desc = $doc.CreateTextNode('1111')
$sref.AppendChild($desc)
# create node <gateway> and append child nodes <hostname> and <port>
$src = $doc.CreateNode('element', 'gateway', '')
$src.AppendChild($comp)
$src.AppendChild($sref)
# append node <gateway> to node <gateways>
$svc = $doc.SelectSingleNode('//gateways')
$svc.AppendChild($src)
# create node <hostname>
$comp = $doc.CreateNode('element', 'hostname', '')
$desc = $doc.CreateTextNode('test2')
$comp.AppendChild($desc)
# create node <port>
$sref = $doc.CreateNode('element', 'port', '')
$desc = $doc.CreateTextNode('2222')
$sref.AppendChild($desc)
# create node <gateway> and append child nodes <hostname> and <port>
$src = $doc.CreateNode('element', 'gateway', '')
$src.AppendChild($comp)
$src.AppendChild($sref)
# append node <Source> to node <Service>
$svc = $doc.SelectSingleNode('//gateways')
$svc.AppendChild($src)
# save XML file
$doc.Save("C:\testnew.xml")
}
我可以使用*lon*
并删除<name>
,但XML中的所有其他内容都会自动填充。我最终可能会遇到*bqq*
和*lon*
在我希望避免的文档中的情况。注意 - 这必须适用于powershell v2.0。
将要编辑的XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<netprobe compatibility="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.itrsgroup.com/GA2011.2-110303/netprobe.xsd">
<selfAnnounce>
<enabled>true</enabled>
<retryInterval>60</retryInterval>
<requireReverseConnection>false</requireReverseConnection>
<probeName>
<hostname />
<data>_</data>
<port />
<data>-SA</data>
</probeName>
<managedEntity>
<name>lonms1122</name>
<attributes>
</attributes>
<types>
</types>
</managedEntity>
<gateways>
<gateway>
</gateway>
</gateways>
</selfAnnounce>
</netprobe>
@Mathias
你的答案很有效。请参阅以下代码
$doc = [xml](Get-Content C:\selfannouncetestnew.xml)
$gateway = switch -Wildcard($doc.SelectSingleNode('//managedEntity/name').InnerText)
{
"lon*" {
# create node <hostname>
$comp = $doc.CreateNode('element', 'hostname', '')
$desc = $doc.CreateTextNode('test')
$comp.AppendChild($desc)
# create node <port>
$sref = $doc.CreateNode('element', 'port', '')
$desc = $doc.CreateTextNode('1111')
$sref.AppendChild($desc)
# create node <gateway> and append child nodes <hostname> and <port>
$src = $doc.CreateNode('element', 'gateway', '')
$src.AppendChild($comp)
$src.AppendChild($sref)
# append node <gateway> to node <gateways>
$svc = $doc.SelectSingleNode('//gateways')
$svc.AppendChild($src)
# create node <hostname>
$comp = $doc.CreateNode('element', 'hostname', '')
$desc = $doc.CreateTextNode('test2')
$comp.AppendChild($desc)
# create node <port>
$sref = $doc.CreateNode('element', 'port', '')
$desc = $doc.CreateTextNode('2222')
$sref.AppendChild($desc)
# create node <gateway> and append child nodes <hostname> and <port>
$src = $doc.CreateNode('element', 'gateway', '')
$src.AppendChild($comp)
$src.AppendChild($sref)
# append node <gateway> to node <gateways>
$svc = $doc.SelectSingleNode('//gateways')
$svc.AppendChild($src)
$doc.Save("c:\selfannouncetestnew.xml")
}
"sto*" {
# create node <hostname>
$comp = $doc.CreateNode('element', 'hostname', '')
$desc = $doc.CreateTextNode('test3')
$comp.AppendChild($desc)
# create node <port>
$sref = $doc.CreateNode('element', 'port', '')
$desc = $doc.CreateTextNode('3333')
$sref.AppendChild($desc)
# create node <gateway> and append child nodes <hostname> and <port>
$src = $doc.CreateNode('element', 'gateway', '')
$src.AppendChild($comp)
$src.AppendChild($sref)
# append node <gateway> to node <gateways>
$svc = $doc.SelectSingleNode('//gateways')
$svc.AppendChild($src)
# create node <hostname>
$comp = $doc.CreateNode('element', 'hostname', '')
$desc = $doc.CreateTextNode('test4')
$comp.AppendChild($desc)
# create node <port>
$sref = $doc.CreateNode('element', 'port', '')
$desc = $doc.CreateTextNode('4444')
$sref.AppendChild($desc)
# create node <gateway> and append child nodes <hostname> and <port>
$src = $doc.CreateNode('element', 'gateway', '')
$src.AppendChild($comp)
$src.AppendChild($sref)
# append node <gateway> to node <gateways>
$svc = $doc.SelectSingleNode('//gateways')
$svc.AppendChild($src)
$doc.Save("c:\selfannouncetestnew.xml")
}
"bqq*" {
"barragw:3456"
}
"hkg*" {
"hongkonggw:4567"
}
default {
"defaultgw:5678"
}
}
$hostname,$port = $gateway -split ':'
# create node <hostname>
$comp = $doc.CreateNode('element', 'hostname', '')
$desc = $doc.CreateTextNode($hostname)
$comp.AppendChild($desc)
# create node <port>
$sref = $doc.CreateNode('element', 'port', '')
$desc = $doc.CreateTextNode($port)
$sref.AppendChild($desc)
$doc.Save("C:\selfannouncetestnew.xml")
为了测试答案的底部,我将名称更改为hkggk1122
,但没有任何反应。如果名称为lon(something)
或sto(something)
,则会添加网关。我想让你的解决方案工作,但不确定你要告诉我你的底部部分。
答案 0 :(得分:2)
预先解析整个文档,找到managementEntity/name
节点并使用switch
来确定网关详细信息:
$xml = [xml](Get-Content C:\testnew.xml)
$gateway = switch -Wildcard($xml.SelectSingleNode('//managedEntity/name').InnerText)
{
"lon*" {
"londongw:1234"
}
"sto*" {
"stockholmgw:2345"
}
"bqq*" {
"barragw:3456"
}
"hkg*" {
"hongkonggw:4567"
}
default {
"defaultgw:5678"
}
}
$hostname,$port = $gateway -split ':'
# Create appropriate childnodes and append here
# create node <hostname>
$comp = $doc.CreateNode('element', 'hostname', '')
$desc = $doc.CreateTextNode($hostname)
$comp.AppendChild($desc)
# create node <port>
$sref = $doc.CreateNode('element', 'port', '')
$desc = $doc.CreateTextNode($port)
$sref.AppendChild($desc)
# and so on ...
答案 1 :(得分:0)
like
参数中有空格。取代
' <name>lon*</name>'
使用:
'<name>lon*</name>'