Xml 如何使用通配符?

Xml 如何使用通配符?,xml,powershell,wildcard,Xml,Powershell,Wildcard,我有插入本地主机名的XML。主机名可以是以下任意一种: lonmq1111 斯托姆1111 BQLK1111 hkgtp1111 根据主机名,下面的脚本需要向XML添加正确的网关。例如: 如果是lon*或sto*那么 添加这些网关 如果是bqq*或hkg*那么 添加不同的网关 埃尔塞夫等。 我有下面的,但它不工作。关于如何在中间使用通配符的想法? $file=Get Content C:\testnew.xml 如果($file-like'lon*'-或'sto*'){ #加载XML文件 [

我有插入本地主机名的XML。主机名可以是以下任意一种:

lonmq1111
斯托姆1111
BQLK1111
hkgtp1111
根据主机名,下面的脚本需要向XML添加正确的网关。例如:

如果是lon*或sto*那么
添加这些网关
如果是bqq*或hkg*那么
添加不同的网关
埃尔塞夫等。
我有下面的,但它不工作。关于如何在中间使用通配符的想法?

$file=Get Content C:\testnew.xml
如果($file-like'lon*'-或'sto*'){
#加载XML文件
[xml]$doc=获取内容“C:\testnew.xml”
#创建节点
$comp=$doc.CreateNode('element','hostname','')
$desc=$doc.CreateTextNode('test'))
$comp.AppendChild($desc)
#创建节点
$sref=$doc.CreateNode('element','port','')
$desc=$doc.CreateTextNode('1111')
$sref.AppendChild($desc)
#创建节点并附加子节点和
$src=$doc.CreateNode('element','gateway','')
$src.AppendChild($comp)
$src.AppendChild($sref)
#将节点追加到节点
$svc=$doc.SelectSingleNode(“//网关”)
$svc.AppendChild($src)
#创建节点
$comp=$doc.CreateNode('element','hostname','')
$desc=$doc.CreateTextNode('test2'))
$comp.AppendChild($desc)
#创建节点
$sref=$doc.CreateNode('element','port','')
$desc=$doc.CreateTextNode('2222')
$sref.AppendChild($desc)
#创建节点并附加子节点和
$src=$doc.CreateNode('element','gateway','')
$src.AppendChild($comp)
$src.AppendChild($sref)
#将节点追加到节点
$svc=$doc.SelectSingleNode(“//网关”)
$svc.AppendChild($src)
#保存XML文件
$doc.Save(“C:\testnew.xml”)
}
我可以使用
*lon*
删除
,但XML中的所有内容都是自动填充的。我可能最终会遇到这样的情况,
*bqq*
*lon*
出现在我想要避免的文档中。注意-这必须适用于powershell v2.0

将要编辑的XML:


真的
60
假的
_
-SA
lonms1122
@马蒂亚斯

你的答案很有用。请参见下面的代码

$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")
$doc=[xml](获取内容C:\selfannouncetestnew.xml)
$gateway=switch-通配符($doc.SelectSingleNode('//managedEntity/name')。InnerText)
{
“lon*”{
#创建节点
$comp=$doc.CreateNode('element','hostname','')
$desc=$doc.CreateTextNode('test'))
$comp.AppendChild($desc)
#创建节点
$sref=$doc.CreateNode('element','port','')
$desc=$doc.CreateTextNode('1111')
$sref.AppendChild($desc)
#创建节点并附加子节点和
$src=$doc.CreateNode('element','gateway','')
$src.AppendChild($comp)
$src.AppendChild($sref)
#将节点追加到节点
$svc=$doc.SelectSingleNode(“//网关”)
$svc.AppendChild($src)
#创建节点
$comp=$doc.CreateNode('element','hostname','')
$desc=$doc.CreateTextNode('test2'))
$comp.AppendChild($desc)
#创建节点
$sref=$doc.CreateNode('element','port','')
$desc=$doc.CreateTextNode('2222')
$sref.AppendChild($desc)
#创建节点并附加子节点和
$src=$doc.CreateNode('element','gateway','')
$src.AppendChild($comp)
$src.AppendChild($sref)
#将节点追加到节点
$svc=$doc.SelectSingleNode(“//网关”)
$svc.AppendChild($src)
$doc.Save(“c:\selfannouncetestnew.xml”)
}
“sto*”{
#创建节点
$comp=$doc.CreateNode('element','hostname','')
$desc=$doc.CreateTextNode('test3'))
$comp.AppendChild($desc)
#创建节点
$sref=$doc.CreateNode('element','port','')
$desc=$doc.CreateTextNode('3333')
$sref.AppendChild($desc)
#创建节点并附加子节点和
$src=$doc.CreateNode('element','gateway','')
$src.AppendChild($comp)
$src.AppendChild($sref)
#将节点追加到节点
$svc=$doc.SelectSingleNode(“//网关”)
$svc.AppendChild($src)
#创建节点
$comp=$doc.CreateNode('element','hostname','')
$desc=$doc.CreateTextNode('test4'))
$comp.AppendChild($desc)
#创建节点
$sref=$doc.CreateNode('element','port','')
$desc=$doc.CreateTextNode('4444')
$sref.AppendChild($desc)
#创建节点并附加子节点和
$src=$doc.CreateNode('element','gateway','')
$src.AppendChild($comp)
$src.AppendChild($sref)
#将节点追加到节点
$svc=$doc.SelectSingleNode(“//网关”)
$svc.AppendChild($src)
$doc.Save(“c:\selfannouncetestnew.xml”)
}
“bqq*”{
“巴拉格:3456”
}
“hkg*”{
“香港:4567”
}
违约{
“默认GW:5678”
}
}
$hostname,$port=$gateway-split':'
#创建节点
$comp=$doc.CreateNode('element','hostname','')
$desc=$doc.CreateTextNode($hostname)
$comp.AppendChild($desc)
#创建节点
$sref=$doc.CreateNode('element','port','')
$desc=$doc.CreateTextNode($port)
$sref.AppendChild($desc)
$doc.Save(“C:\selfannouncetestnew.xml”)

为了测试你答案的下半部分,我将名字改为
hkggk1122
,但什么也没发生。如果名称为
lon(something)
sto(something)
则会添加网关。我想让您的解决方案起作用,但不确定您试图告诉我如何处理底部零件。

您的
like
参数中有一个空格。替换

' <name>lon*</name>'
“lon*”
与:

'lon*'

预先解析整个文档,找到
管理实体/名称
节点,并使用
开关
确定网关详细信息:

$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=[xml](获取内容C:\testnew.xml)
$gateway=switch-Wildcard($xml.SelectSingleNode('//managedEntity/name')。InnerText)
{
“lon*”{
“伦敦W:1234”
}
“sto*”{
"总人口:2345"
}
“bqq*”{
“巴拉格:3456”
}
“hkg*”{
“香港:4567”
}
违约{
“默认GW:5678”
}
}
$hostname,$port=$gateway-split':'
#创建适当的子节点并附加到此处
#创建节点
$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 ...