Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell编辑XML选择器_Xml_Powershell - Fatal编程技术网

使用powershell编辑XML选择器

使用powershell编辑XML选择器,xml,powershell,Xml,Powershell,好吧,我的powershell和脚本编写技能非常简单,请耐心听我说。我在多个服务器上有一个.xml文档,其中有几个节点需要更改,而不需要更改整个xml。我试图创建一个简单的命令/批处理或脚本来更改这些模式,而不是打开并手动编辑它们。以下是需要更改的内容: <selector> <ip address=“x.x.x.x” allow-undefined=“no” /> </selector> 我需要把它改成 <selector> <inte

好吧,我的powershell和脚本编写技能非常简单,请耐心听我说。我在多个服务器上有一个.xml文档,其中有几个节点需要更改,而不需要更改整个xml。我试图创建一个简单的命令/批处理或脚本来更改这些模式,而不是打开并手动编辑它们。以下是需要更改的内容:

<selector>
<ip address=“x.x.x.x” allow-undefined=“no” />
</selector>

我需要把它改成

<selector>
<interface id=“listener” />
</selector>

然后是一个节点

<selector>
<user-group name=“servername\SSH Access” />
</selector>

最后同上

<selector>
<user-group name=“servername\SSH Access” />
</selector>

谢谢你的帮助

$xml=[xml]@"
<?xml version="1.0" encoding="UTF-8"?>
   <selector>
     <ip ipaddress="123" allowundefined="no" />
   </selector>
"@

$xml.selector.RemoveChild($xml.selector.LastChild)
$ele=$xml.CreateElement("interface")
$attri=$xml.CreateAttribute("id")
$attri.Value="listener"
$ele.Attributes.Append($attri)
$xml.DocumentElement.AppendChild($ele)
供您参考

您可以尝试以上内容,并根据需要对其进行修改。
你会从试错中得到很多想法

你试过什么代码?你知道你可以通过[XML]$Document=Get Content File.XML来读取XML对象吗?这是我的愚蠢之处,它怎么知道打开哪个XML文件?[filename.xml]?!?
<selector>
<user-group name=“servername\SSH Access” allow-undefined=“no” />
</selector>
$xml=[xml]@"
<?xml version="1.0" encoding="UTF-8"?>
   <selector>
     <ip ipaddress="123" allowundefined="no" />
   </selector>
"@

$xml.selector.RemoveChild($xml.selector.LastChild)
$ele=$xml.CreateElement("interface")
$attri=$xml.CreateAttribute("id")
$attri.Value="listener"
$ele.Attributes.Append($attri)
$xml.DocumentElement.AppendChild($ele)