Tuesday, October 07, 2014

Powershell and xml

For future reference
script to read properties from a xml file and update specified values.
More of a proof of concept than any real world usage.
Good for any cocktailparty
----------------xmllooptest.ps1 ---------
$path = split-path -parent $MyInvocation.MyCommand.Definition
$inputfile = $path + "\myinputfile3.xml"
[xml]$xmlinput = (Get-Content $inputFile)

write-host $xmlinput.main.configuration.name" is my value"
write-host "Server is:"$xmlinput.main.configuration.server
write-host "Result : "$xmlinput.main.configuration.result


Write-host "Looping sites..."
$sites = $xmlinput.main.sites.site
foreach ($site in $sites)
{
    write-host "-----"
    write-host $site.name
    write-host $site.property
    if ($site.name -match "my site 2")
    {
    $site.property = "New property for site2"
    }
    write-host $site.property
   
}
$xmlinput.save($inputfile)
-----------------------eof-----------------
----------------- myInputfile3.xml------------


 
    myserver
    Wyoming
    Testing
 

 
   
      my site 1
      My even newer property
   

   
      my site 2
      New property for site2
   

 
--------------------eof--------------

No comments: