如何使用通配符?
How to use Wildcards?
我有 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>
@马蒂亚斯
您的回答很有效。见下方代码
$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)
,它会添加网关。我想让你的解决方案起作用,但不确定你想告诉我用底部做什么。
您的 like
参数中有一个 space。替换
' <name>lon*</name>'
与:
'<name>lon*</name>'
预先解析整个文档,找到 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 ...
我有 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>
@马蒂亚斯
您的回答很有效。见下方代码
$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)
,它会添加网关。我想让你的解决方案起作用,但不确定你想告诉我用底部做什么。
您的 like
参数中有一个 space。替换
' <name>lon*</name>'
与:
'<name>lon*</name>'
预先解析整个文档,找到 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 ...