Update XML file remotely using PowerShell

Updating XML file is pretty simple. The more difficult part is to do this remotely and create nice formatted output. In this article I will try to explain how to do this in simple way.

Updating XML file

Lets say that structure of our Web.Config XML file will look like this and we want to check value from Encryption key:

Configuration -> AdfsConfiguration -> Certificate -> Add

xml
xml

To import file we can use Get-Content command where we have to specify web.config file path. Remember to add [ xml ] type before variable name [ xml ]$XmlDocument:

#Import config file
[ xml ]$XmlDocument = Get-Content -Path "D:\apps\web.config"

#Navigate to "key"
$value = $XmlDocument.Configuration.AdfsConfiguration.Certificate.add | Where-Object {$_.Key -eq "encryption"}

#Display key value
$value.value

To check value remotely and export results to CSV file we just have to use Invoke-Command:

#List of the servers
$Servers = Get-Content "C:\users\$env:username\desktop\Servers.txt"
$Array = @()

#Looping each server
Foreach($Server in $Servers)
{
    $Check = $null
    $XmlDocument = $null
    $value = $null

    $Server = $Server.trim()

    # Checking key value on remote server     
    $Check = Invoke-Command $Server -ScriptBlock{
       
            [ xml ]$XmlDocument = Get-Content -Path "D:\apps\web.config";

            $value = $XmlDocument.Configuration.AdfsConfiguration.Certificate.add | Where-Object {$_.Key -eq "encryption"};
            $value.value
    }

    If( $Check )
    {
         Write-Host $Server - $Check
         # Adding value to an object
         $Object = New-Object PSObject -Property ([ordered]@{ 
    
                "Server name"             = $Server
                "Thumbprint"              = $Check
        })
 
        # Add object to our array
        $Array += $Object 
    }
}

If($Array)
{
    # Exporting results to CSV
    $Array | Export-Csv -Path C:\users\$env:username\desktop\results.csv -NoTypeInformation
}

thumbprint
thumbprint

To change Encryption key value we can use following scripts:

Localy:

  $Thumbprint = "5555666677777xxxxvvvvnn1111"
  [ xml ]$XmlDocument = Get-Content -Path "D:\apps\web.config"
  $value = ($XmlDocument.Configuration.AdfsConfiguration.Certificate.Add | Where-Object {$_.Key -eq "encryption"})

  # Change encryption value
  $value.value = $Thumbprint
  $value.value

Remotely:

      $Server = "ADFS01"
      $Thumbprint = "5555666677777xxxxvvvvnn1111"
      $WebValue = Invoke-Command $Server -ScriptBlock{param($Thumbprint)
       
            [ xml ]$XmlDocument = Get-Content -Path "D:\apps\web.config";

            $value = $XmlDocument.Configuration.AdfsBridgeConfiguration.Certificate.Add | Where-Object {$_.Key -eq "encryption"};

            # Change encryption value
            $value.value = $Thumbprint;
            $value.value

        } -ArgumentList $Thumbprint

        $WebValue

In my case after the value change I had to reset IIS additionally:

Invoke-Command $Server -ScriptBlock{ iisreset | Where-Object {$_ -match "Internet services"} }

Final output should look like this:

XML results
XML results

Final script:


$Servers = Get-Content "C:\users\$env:username\desktop\Servers.txt"
$Thumbprint = "5555666677777xxxxvvvvnn1111"
$Array  = @()

Foreach($Server in $Servers)
{
    $Server = $Server.trim()
    $CheckPath = $null
    $Object = $null
    $XmlDocument = $null
    $IIS = $null
    $iisreset = $null

    $CheckPath = Test-Path "\\$Server\d$\apps\web.config"

    If($CheckPath -match "false")
    {
        Write-Host "$Server - Failed to connect"
        
        $Object = New-Object PSObject -Property ([ordered]@{ 
    
                "Server name"             = $Server
                "Status"                  = "Offline"
                "Thumbprint"              = "(null)"
                "IIS Reset"               = "(null)"
        })
 
        # Add object to our array
        $Array += $Object 
    }
    Else
    {
        $WebValue = Invoke-Command $Server -ScriptBlock{param($Thumbprint)
       
            [ xml ]$XmlDocument = Get-Content -Path "D:\apps\web.config";

            $value = $XmlDocument.Configuration.AdfsBridgeConfiguration.Certificate.add | Where-Object {$_.Key -eq "encryption"};
            # Change encryption value
            $value.value = $Thumbprint;
            $value.value 

        } -ArgumentList $thumbprint

        Write-Host $Server - $WebValue

        $IIS = Invoke-Command $Server -ScriptBlock{ iisreset | Where-Object {$_ -match "Internet services"} }
        
        If($IIS -match "succesfully restarted")
        {
            $IISreset = "True"
        }
        Else
        {
            $IISreset = "False"
        }
            
        $Object = New-Object PSObject -Property ([ordered]@{ 
    
                "Server name"             = $Server
                "Status"                  = "Online"
                "Thumbprint"              = $WebValue
                "IIS Reset"               = $IISreset
        })
 
        # Add object to our array
        $Array += $Object 
    }
}

If($Array)
{
    $Array | Format-Table -AutoSize -Wrap
}  

I hope that this was informative for you. See you in next articles 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.