How to read XML file from web using powershell

Tags: xml powershell
Question!

When I connect to my HP ILO using the URL, http://ilo-ip/xmldata?item=All it is returning a XML file in below format.

  <?xml version="1.0" ?> 
- <RIMP>
- <HSI>
  <SPN>ProLiant DL385 G1</SPN> 
  <SBSN>ABCDEFG</SBSN> 
  <UUID>123456789</UUID> 
  </HSI>
- <MP>
  <ST>1</ST> 
  <PN>Integrated Lights-Out (iLO)</PN> 
  <FWRI>1.82</FWRI> 
  <HWRI>ASIC: 2</HWRI> 
  <SN>ILOABCDEFG</SN> 
  <UUID>ILO1234545</UUID> 
  </MP>
  </RIMP>

Is there any powershell command/script to read XML data from web URL?



Answers
PowerShell uses the .Net framework XmlDocument so you could call the load method to load the xml into an XmlDocument object like this:

#Option 1    
$doc = New-Object System.Xml.XmlDocument
$doc.Load("http://ilo-ip/xmldata?item=All")

#Option 2
[xml]$doc = (New-Object System.Net.WebClient).DownloadString("http://ilo-ip/xmldata?item=All")


# Your XML will then be in the $doc and the names of the XML nodes can be used as
# properties to access the values they hold. This short example shows how you would
# access the value held in the SPN nodes from your sample XML 
Write-Host $doc.RIMP.HSI.SPN

If your looking for more information on working with the XML once it has been loaded check out Dr. Tobias Weltner's eBook at PowerShell.com, chapter 14 covers XML.



Sure. You can use the .NET WebClient object to download XML from the web and then use the [xml] type to interpret a string as XML like so:

$url = "http://blogs.msdn.com/powershell/rss.xml"
[xml]$xml = (new-object System.Net.WebClient).DownloadString($url)
$xml.rss.channel | Foreach {$_.item} |
    Where {[DateTime]$_.pubDate -gt (Get-Date).AddMonths(-1)} |
    Format-Table Title


This video can help you solving your question :)
By: admin