Parse GPResult.html file using Powershell

by v-2nas at 2013-01-01 22:48:15

Hi All,

I have taken up a task to parse gpresult (Local/Audit Policies) to a csv file however I am not quite familiar with parsing techniques involving complex html codes.

So far from my internet search i have got the following code but beyond that i don’t know how to proceed.

$ie = new-object -com “InternetExplorer.Application”
# The easiest way to accomodate for slowness of IE
Start-Sleep -Seconds 1
$ie.Navigate(“C:\GPReport.htm”)
# The easiest way to accomodate for slowness of IE
Start-Sleep -Seconds 1
$ParsedHtml = $ie.Document


So basically what i am trying to do is to dump gpresult into a html file using cmd
<i> gpresult /s remoteComputerName /h gpresult.html /scope computer</i>

The command will run on multiple computers and generate gpresult per computer. Later i need to parse html file from multiples computer and dump it into a csv file with following headers
computername, policy, setting, winning gpo

attached is gpresult.html for your reference.

Your help to get to resolution is greatly appreciated.

Regards,
Nav
by DonJ at 2013-01-02 07:37:12
This isn’t really a PowerShell question; you’re dealing with the Document Object model in IE. I’d refer you to http://www.w3schools.com/htmldom/default.asp for a tutorial, and warn you that accessing a DOM through the IE COM object is a grade-A pain in the butt.

That said, your HTML is actually XHTML, which means it should validate as an XML document.


[xml]$doc = Get-Content rsop.html


Retrieving data from XML is a lot more straightforward and doesn’t involve the stupid IE COM object. PowerShell can do XML natively. You can execute XPath queries, enumerate through the XML document as a collection of objects, and much more. You may have to learn some new skills to accomplish all this, but they’re much more worth learning than the IE COM object and its DOM.
by megamorf at 2013-01-03 08:57:47
Why don’t you just export is as XML and parse it that way? I did that with our resultancy sets of policies.