Tuesday, January 13, 2009

PowerShell and XML

After my last post, I thought it might be a good idea to spend a little more time explaining the XML capabilities of PowerShell. I had 2 people ask for clarification on it so I obviously didn't give it the attention it deserved! Following are a few examples of what can be done with the XML type in PowerShell.

The file that we will be using is from a Microsoft Baseline Security Analysis. To follow along with the examples, it would be beneficial if you had a similar file. You can use this sample. It looks something like this.


The first thing we need to do is explicitly cast it as an XML file:
[XML]$ScanResult = Get-Content 'c:\sampleMBSA.xml'

Now that we have the XML file, lets see what we can do with it.

  1. Get the Server (Machine) name:
    PS> $File.SecScan.Machine
    PS> Test-VP01
  2. Get the IP address:
    PS> $file.SecScan.IP
    PS> 12.345.67.890
  3. What 3 areas were scanned:
    PS> $file.SecScan.Check Select Name
    Name
    Office Security Updates
    SDK Components Security Updates
    Windows Security Updates
  4. See the complete list of updates (installed or not) for Office Security Updates:
    Note that we had 3 areas that were scanned, we are looking at the first one, so we start with [0] index.
    PS> $file.SecScan.Check[0].Detail.UpdateData Select Title
    Title
    Office 2003 Service Pack 2 for Proofing Tools
    Security Update for Office 2003 (KB920813)
    Security Update for Excel 2003 (KB925257)
    Security Update for Word 2003 (KB923094)
    Visio 2003 Service Pack 2
    Security Update for Office 2003 (KB924424)
    ....
  5. See the complete list of updates that need installed for Office Security Updates:
    PS> $file.SecScan.Check[0].Detail.UpdateData `
    > Where{$_.IsInstalled -eq $false} `
    > Select Title
    Office 2003 Service Pack 2
    Security Update for Office 2003 (KB954478)
    Security Update for Office 2003 (KB923272)
    Security Update for PowerPoint 2003 (KB9230
    Security Update for Microsoft Office Excel
    Security Update for Office 2003 (KB936048)
    Security Update for Excel 2003 (KB940602)
    Outlook Live 2003 Service Pack 2
    Project 2003 Service Pack 2
    Security Update for Office 2003 (KB924424)
    ...
  6. See the list of critical updates that need to be installed for Office Security Updates:
    PS> $file.SecScan.Check[0].Detail.UpdateData `
    > where{$_.IsInstalled -eq $false -and $_.Severity -eq 4} `
    > select Title, @{name="Severity";Expression={"Critical"}}
    Severity Title
    Critical Security Update for Access Snapshot Viewer 2003 (KB955439)
    Critical Security Update for Microsoft Office Outlook 2003 (KB945432)

Hopefully, this clarifies some of possibilities with XML and PowerShell.

Enjoy!

No comments: