我正在尝试从多台计算机上的配置文件中获取值。
这是每台计算机上的文件...我试图将 \gm107a\Updates\QC 与计算机名称一起放入 csv 文件中。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="updatelocation" value="\\gm107a\Updates\QC"/>
<add key="filename" value="QualityControl.exe"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我发现这让我开始: https: //social.technet.microsoft.com/Forums/scriptcenter/en-US/268e03cb-1248-456f-bc89-ecc31cb0489b/powershell-script-to-read-xml-data-然后from-multiple-remote-computers在 powershell 中发现了这个获取 app.config 元素并尝试将其放在一起......记住我是全新的哈哈。
这是我到目前为止所拥有的:
function vert {
$hostnamenodes = get-content C:\Scripts\Computers.txt
foreach ($hostname in $hostnamenodes) {
[xml]$xml = Get-Content \\$hostname\C$\Drivers\Version.config
#Add hostrecord to array
$MasterArray = New-Object psobject -Property @{
"ServerName" = $hostname
"updatelocation" = $xml.SelectNodes('//add[@key="updatelocation"]/@value')[0].'#text'
}
write-output $masterarray
} }
vert | select servername,updatelocation | Export-Csv QA.csv -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Unable to index into an object of type System.Xml.XPathNodeList.
At C:\Scripts\get-content.ps1:13 char:88
+ "updatelocation" = $xml.SelectNodes('//add[@key="updatelocation"]/@value')[ <<<< 0].'#text'
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
Run Code Online (Sandbox Code Playgroud)
使用下面的解决方案编辑脚本后,我现在收到以下错误:
这是我收到的新错误:
Unable to index into an object of type System.Xml.XPathNodeList.
At C:\Scripts\get-content.ps1:9 char:92
+ "updatelocation" = $xml.SelectNodes('//add[@key="updatelocation"]/@value')[ <<<< 0].'#text'}
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
Run Code Online (Sandbox Code Playgroud)
小智 5
好的,尝试了不同的方法,它有效!!!!耶!感谢您帮助检查错误!
"updatelocation" = $xml.configuration.appSettings.add | Where-Object { $_.key -eq 'updatelocation' } | Select-Object -ExpandProperty value
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8215 次 |
| 最近记录: |