user4447529
user4447529

Reputation:

Get appSettings key value in Powershell

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

Upvotes: 3

Views: 8533

Answers (2)

user4447529
user4447529

Reputation:

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

Upvotes: 5

Jan Chrbolka
Jan Chrbolka

Reputation: 4454

Your code works fine with the supplied Version.config example (just missing a closing ">" at the end. From the error it looks you're hitting an empty Version.config file or a file with missing "updatelocation" key. Should do some error checking...

function vert {
    $hostnamenodes = get-content C:\Scripts\Computers.txt
    foreach ($hostname in $hostnamenodes) {
        if (test-path \\$hostname\C$\Drivers\Version.config){
            [xml]$xml = Get-Content \\$hostname\C$\Drivers\Version.config
            if ($xml.SelectNodes('//add[@key="updatelocation"]/@value')) {
                $MasterArray = New-Object psobject -Property @{
                "ServerName" = $hostname
                "updatelocation" = $xml.SelectNodes('//add[@key="updatelocation"]/@value')[0].'#text'}
            } else {
                write-error "Key `"updatelocation`" does not exist"
            }
        } else {
            write-error "File `"Version.config`" does not exist"
        }
        write-output $masterarray
    }  
}

vert | select servername,updatelocation | Export-Csv QA.csv -NoTypeInformation

Upvotes: 0

Related Questions