I am trying to get a value from a config file on multiple computers.
Here is the file on each computer.... I am trying to get \gm107a\Updates\QC into a csv file along with the computer name.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="updatelocation" value="\\gm107a\Updates\QC"/>
<add key="filename" value="QualityControl.exe"/>
</appSettings>
</configuration>
I found this to start me going: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/268e03cb-1248-456f-bc89-ecc31cb0489b/powershell-script-to-read-xml-data-from-multiple-remote-computers then found this getting app.config elements in powershell and tried putting it together.... Remember I am brand new lol.
Here is what I have so far:
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
Here is the error I get:
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
After editing my script with the solution below I now get the following error:
Here is the new error I am getting:
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
Ok tried it a different way and it works!!!! Yay! Thanks for your help with error checking!!!
"updatelocation" = $xml.configuration.appSettings.add | Where-Object { $_.key -eq 'updatelocation' } | Select-Object -ExpandProperty value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With