Thursday, November 20, 2008

How I used PowerShell to make my manager happy

 

How I used PowerShell to make my manager happy

Microsoft loves metrics. We have to report something as a number, bigger than last month, smaller than last month. Whatever!!! So the challenge I faced was how to find out how many people accessed my SharePoint site. That sounds easy right? Go to SharePoint, enable the statistics and read off the number. But what if the Site runs a custom C# application that dynamically populates the page I want to measure with information from more than one list? The stat I get is x number of people hit this page. Doesn’t tell me what was on the page.

Fortunately I have all the pieces. The IIS log gives me the ID for the primary list used in the Stem information. So now I can get a little more granular, x people hit this page for this ID. But a 4 digit code is no good to my manager and her manager. What I need is a script that will parse the IIS logs for a month, pull out all the accesses to my content page, that that ID and look it up and return the details.

I thought of Perl as first choice to parse the log, but the look up was the tricky part. I knew I could get a master list of IDs into an XML file and after research I found I could do the whole look up in one line.

So how did I go it?

First off, find all the files I need to look at.

I used Get-ChildItem for a the folder and started the master loop using a foreach loop.

foreach ($file in $folder)

Notice the original use of names?

Next I needed to get the file content and match the three main criteria, Page name, GET verb and a successful access at that.

    $filedata = Get-Content $file.FullName 
    foreach ($row in $filedata)
    {
        if ($row -match "SessionDetails")
        {
            if ($row -match "GET")
            {
                if ($row -match " 200 ")

Next I used a feature from the PowerShell Community Extensions to break up the log line and extract the ID I needed

details = Split-String $row -Separator " "
$ID = $details[6].Substring($details[6].LastIndexOf("=")+1)

Finally the bit it was all for, parsing an XML to get the entry

$title = Select-Xml SessionList.xml -XPath "//z:row[@ows_ID=$ID]/@ows_LinkTitle"

I now have the title of the page, so now I can safely say, x number of people access y title over the last month. I now have this simple script that can run month on month. I didn’t think it could be done in these few lines, but PowerShell proved me wrong.

Alan's World of IT : How I used PowerShell to make my manager happy

No comments:

Blog Archive