类型为System.Xml.XmlElement的Powershell格式输出

|| 我正在尝试构建计算机名列表,然后可以使用这些计算机名来调用另一个powershell命令。 手动流程:
$Type1Machines=\"Machine1\",\"Machine2\",\"Machine3\",\"Machine4\"
Invoke-command {Powershell.exe C:\\myscript.ps1 Type1} -computername $Type1Machines
我已经在XML文件(MachineInfo.xml)中获得有关\“ Type1 \”机器名称的信息
<Servers>
<Type1>
<Machine><Name>Machine1</Name> <MachineOS>WinXP</MachineOS></Machine>
<Machine><Name>Machine2</Name> <MachineOS>WinServer2003</MachineOS></Machine>
<Machine><Name>Machine3</Name> <MachineOS>WinServer2003</MachineOS></Machine>
<Machine><Name>Machine4</Name><MachineOS>WinServer2003</MachineOS></Machine>
</Type1>
</Servers>
我正在尝试编写一个脚本,该脚本可以提取机器名称为\“ Type1 \”的列表并构造以下URL。   $ Type1Machines = \“ Machine1 \”,\“ Machine2 \”,\“ Machine3 \”,\“ Machine4 \” 到目前为止,我已经可以从xml中获取计算机名称列表了
    #TypeInformation will be pass as an argument to the final script
    $typeinformation = \'Type1\' 
$global:ConfigFileLocation =\"C:\\machineinfo.xml\"
$global:ConfigFile= [xml](get-content $ConfigFileLocation)

$MachineNames = $ConfigFile.SelectNodes(\"Servers/$typeinformation\")
$MachineNames
输出:
Machine
-------
{Machine1, Machine2, Machine3, Machine4}
现在如何使用上面的输出并构造下面的URL? $ Type1Machines = \“ Machine1 \”,\“ Machine2 \”,\“ Machine3 \”,\“ Machine4 \” 任何帮助表示赞赏。谢谢你的时间!     
已邀请:
我假设您希望将每个计算机名称值放入数组(以与invoke-commmand一起使用):
[string[]]$arr = @() # declare empty array of strings
$ConfigFile.SelectNodes(\"/Servers/$typeInformation/Machine\") | % {$arr += $_.name}
    
接受的解决方案(已复制):
[string[]]$arr = @() # declare empty array of strings
$ConfigFile.SelectNodes(\"/Servers/$typeInformation/Machine\") | % {$arr += $_.name}
具有以下等效功能(更多PowerShell方式,仅在必要时使用.NET):
$typeInformation = \'Type1\'
$arr = ($ConfigFile.Servers.\"$typeInformation\".Machine | % { $_.Name }) -join \',\'
要么
$typeInformation = \'Type1\'
$arr = ($ConfigFile | Select-Xml \"/Servers/$typeInformation/Machine/Name\" | % { $_.Node.\'#text\' }) -join \',\'
    
这是您的代码:您只是在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
}
    

要回复问题请先登录注册