【发布时间】:2017-03-12 17:39:58
【问题描述】:
param
(
$xmlFile="D:\Servers.xml"
)
Import-Module WebAdministration
[xml]$xmlDoc = Get-Content -Path $xmlFile
for ($i=0; $i -lt $xmlDoc.root.Servers.Server.Length; $i++)
{
write-host "Server $i = " $xmlDoc.root.Servers.Server[$i].Name
}
#--------------------- XML ---------------#
<xml>
<root>
<Servers>
<Server><ip>10.2.2.1</ip><website>abc</website>
</Server>
....
<Server><ip>10.2.2.2</ip><website>pqr</website>
</Server>
</Servers>
</root>
</xml>
Powershell: 问题: 问题是如果只有一个服务器节点,“$xmlDoc.root.Servers.Server[$i]”不起作用,因为对象被认为是单一的。除了写额外的 if 之外,我们有什么办法可以解决这个问题。基本上我想遍历 xml 文件并为每个服务器做一些操作。
谢谢, 哈努曼特
【问题讨论】:
标签: xml powershell