[英]Trying to change values within an XML file with Powershell
I have an XML file that I need to programmatically change with Powershell. 我有一个XML文件,需要使用Powershell进行编程更改。 I have searched throughout this site and Google, and none of the answers I've found have worked.
我已经在整个网站和Google上进行了搜索,但没有找到有效的答案。
The XML looks like this: XML如下所示:
<Terminal>
<ID>13</ID>
<TerminalType>0</TerminalType>
<Name>T13 TA BAR </Name>
<StoreID>1</StoreID>
<IPAddress>10.10.10.101</IPAddress>
<SubnetMask>255.255.255.0</SubnetMask>
<DHCP>false</DHCP>
<Workgroup>*NONE</Workgroup>
<CreateBootDrvShare>true</CreateBootDrvShare>
<TimeZone>Eastern Standard Time</TimeZone>
<DaylightSavings>true</DaylightSavings>
<TerminalReload>true</TerminalReload>
<NumTerms>-1</NumTerms>
<Status>1</Status>
<EnableVNC>false</EnableVNC>
<StandaloneInterfaceServer>false</StandaloneInterfaceServer>
<AKTerminalPrefix>-1</AKTerminalPrefix>
<AKUseTouch>false</AKUseTouch>
</Terminal>
There will be a number of these blocks within the XML document, one for each "computer" being used. XML文档中将有许多这样的块,每个“计算机”都使用一个。 I need to be able to change the IP address based on the ID of terminal.
我需要能够根据终端的ID更改IP地址。 How would I go about this?
我将如何处理?
I tried the following, which did not work: 我尝试了以下操作,但无效:
$xml = [xml](Get-Content $file)
$xml.SelectNodes("//Terminal")
Also: 也:
$xml.Terminal | Where-Object {$_.Terminal.ID -eq '1' } | Select-Object IPAddress
Which also did not work. 这也没有用。
If say the ID of the "computer" is 5, I want the IP address to be: 如果说“计算机”的ID为5,我希望IP地址为:
<IPAddress>10.10.10.105</IPAddress>
Any help would be appreciated. 任何帮助,将不胜感激。
Thanks 谢谢
One of your examples is really close to working. 您的例子之一真的很有效。 You can just do the following:
您可以执行以下操作:
$XMLOutputFile = "Path\file.xml"
$xml = [xml](Get-Content $file)
($xml.Terminal | Where-Object {$_.ID -eq '5' }).IPAddress = '10.10.10.105'
$xml.Save($XMLOutputFile)
Once you have an XML object (using the [xml]
type accelerator in this case), you can just update properties using the $object.property = value
syntax. 一旦有了XML对象(在这种情况下,使用
[xml]
类型加速器),就可以使用$object.property = value
语法更新属性。 Then all that is left is handling the output. 然后剩下的就是处理输出。
.Save()
saves the XML document to file. .Save()
将XML文档保存到文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.