System.Xml.XmlElement类型的Powershell格式输出
我正在尝试构造一个计算机名列表,然后可以使用该列表调用另一个powershell命令 手动过程:System.Xml.XmlElement类型的Powershell格式输出,xml,powershell,Xml,Powershell,我正在尝试构造一个计算机名列表,然后可以使用该列表调用另一个powershell命令 手动过程: $Type1Machines="Machine1","Machine2","Machine3","Machine4" Invoke-command {Powershell.exe C:\myscript.ps1 Type1} -computername $Type1Machines 我已经在一个XML文件(MachineInfo.XML)中获得了关于“Type1”机器名称的信息 输出: Machi
$Type1Machines="Machine1","Machine2","Machine3","Machine4"
Invoke-command {Powershell.exe C:\myscript.ps1 Type1} -computername $Type1Machines
我已经在一个XML文件(MachineInfo.XML)中获得了关于“Type1”机器名称的信息
输出:
Machine
-------
{Machine1, Machine2, Machine3, Machine4}
现在我如何使用上面的输出并构造下面的url
$Type1Machines=“Machine1”、“Machine2”、“Machine3”、“Machine4”
感谢您的帮助。谢谢你的时间 我假设您希望将每个机器名值放入数组中(与invoke commmand一起使用):
这是您的代码:您刚刚忘记了Xpath查询中的“Machine”
#TypeInformation will be pass as an argument to the final script
$typeinformation = 'Type1'
$global:ConfigFileLocation ="C:\machineinfo.xml"
$global:ConfigFile= [xml](get-content $ConfigFileLocation)
$Machines = $ConfigFile.SelectNodes("Servers/$typeinformation/Machine")
foreach($Machine in $Machines)
{
Write-Host $Machine.name
}
接受的解决方案(副本):
有这样的等价物(更为强大的方式,仅在必要时使用.NET):
或
我喜欢这个解决方案。比将结果分配给变量并使用for循环提取所需信息要简单得多。谢谢你的帮助。我应该开始使用这种风格的脚本。
Machine
-------
{Machine1, Machine2, Machine3, Machine4}
[string[]]$arr = @() # declare empty array of strings
$ConfigFile.SelectNodes("/Servers/$typeInformation/Machine") | % {$arr += $_.name}
#TypeInformation will be pass as an argument to the final script
$typeinformation = 'Type1'
$global:ConfigFileLocation ="C:\machineinfo.xml"
$global:ConfigFile= [xml](get-content $ConfigFileLocation)
$Machines = $ConfigFile.SelectNodes("Servers/$typeinformation/Machine")
foreach($Machine in $Machines)
{
Write-Host $Machine.name
}
[string[]]$arr = @() # declare empty array of strings
$ConfigFile.SelectNodes("/Servers/$typeInformation/Machine") | % {$arr += $_.name}
$typeInformation = 'Type1'
$arr = ($ConfigFile.Servers."$typeInformation".Machine | % { $_.Name }) -join ','
$typeInformation = 'Type1'
$arr = ($ConfigFile | Select-Xml "/Servers/$typeInformation/Machine/Name" | % { $_.Node.'#text' }) -join ','