gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:21:15 26 4
gpt4 key购买 nike

我正在尝试构建一个计算机名列表,然后可以使用它来调用另一个 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="机器 1","机器 2","机器 3","机器 4"

感谢任何帮助。感谢您的宝贵时间!

最佳答案

接受的解决方案(复制):

[string[]]$arr = @() # declare empty array of strings
$ConfigFile.SelectNodes("/Servers/$typeInformation/Machine") | % {$arr += $_.name}

有这个等价物(更多 PowerShellish 方式,仅在必须时才使用 .NET):

$typeInformation = 'Type1'
$arr = ($ConfigFile.Servers."$typeInformation".Machine | % { $_.Name }) -join ','

$typeInformation = 'Type1'
$arr = ($ConfigFile | Select-Xml "/Servers/$typeInformation/Machine/Name" | % { $_.Node.'#text' }) -join ','

关于xml - System.Xml.XmlElement 类型的 Powershell 格式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5681261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com