Sign In
 
 
Go Search
 
Zach Rosenfield's SharePoint Blog > Posts > Web-based Monitoring (With PowerShell and PowerGadgets)
Web-based Monitoring (With PowerShell and PowerGadgets)

This post is an attempt to show a very cool yet useful use of PowerShell. I will be leveraging PowerGadgets in this demo, but it is certainly not a requirement—and I'll also be covering file upload to SharePoint (via PowerShell), scheduling PowerShell scripts, and some other cool tricks. *As a warning, nothing here has been tested on a large scale, live farm, so use it at your own risk!

My goal for the day—Create a method of visually showing monitoring data in Central Admin. For my example, I went with the number of Site Collections created in the last 6 days. As a teaser, here is my final result:

Step 1: The Script

First I had to create my script to collect the data I needed. Before you go off and make fun of my code, be warned this was not the main goal of my project and I didn't spend much time figuring out the best way to get the data I needed. This is the main area where improvements need to be made before running this particular script on a live farm—and if anyone has ideas on monitoring data they want to collect, post them to the comments below and I can work on how to best get the information. The whole script is available in the Script Library, but I'll try to break out the sections below:

First, I wrote a couple functions to help get at SharePoint data more easily:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#setup STSADM var
$stsadm = "$env:programfiles\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE"

#Function to get Web Apps
function Get-SPWebApplication($name){
$farm = [microsoft.sharepoint.administration.spfarm]::local
$webservice = New-Object microsoft.sharepoint.administration.spwebservicecollection($farm)

if($name -eq $null){
#return all 
    $webservice | %{$_.WebApplications } | Write-Output
}else{
#get one...
$webservice | %{$_.WebApplications } | Where{$_.Name -eq $name}| Write-Output
}
}

Next I needed a script block which would actually collect the data, and return it to whatever 'drawing' mechanism I use. In this example, I constructed a new 'object' that has two fields: Date and Count. I am simply checking each site collection to see if it was created in the last six days, and if so, adding +1 to the corresponding date.

$global:days = 6;
$global:tracker = 0,0,0,0,0,0
$creationDate =
    #Get all web applications (or the one specified) 
    Get-SPWebApplication $vars['name'] | %
    $x=""
     #Get all site collections 
     $_.Sites | %
        #Store Data 
        if($_.CertificationDate.ToLocalTime() -gt ($(Get-Date).AddDays(-5)) ){ 
         for($i=0;$i -lt $days;$i++){ 
         $x = "FOOBAYR" 
         if($_.CertificationDate.ToLocalTime().ToShortDateString() -eq ($(Get-Date).AddDays(-$i).ToShortDateString()) ){ 
             $global:tracker[$i] += 1; 
            } 
         } 
        } 
        $_.Dispose(); 
     } 
    } 

    
for($i=0;$i -lt $days;$i++){ 
     $object = new-object object 
     Add-Member -inputobject $object -membertype NoteProperty -Name Date -Value ($(Get-Date).AddDays(-$i).ToShortDateString()) 
     Add-Member -inputobject $object -membertype NoteProperty -Name Count -Value $tracker[$i]
Write-Output $object 
    }
}

That's the 'heart' of the script! In the next section we'll discuss how I charted the data using PowerGadgets.

Step 2: Configuring PowerGadgets

PowerGadgets is a very cool product. I highly recommend taking a look at all the rich functionality, but for this post I'm simply using the ability to output a chart as a picture. To do this, I first ran the "Out-Chart" PowerGadgets cmdlet with the '-configure' parameter to get a chance to manually alter the look and feel of my chart:

&$creationDate | Out-Chart –configure

Once I felt I had the look and feel all ironed out, I saved that design as a template and the final command I added to my script looks like this:

&$creationDate | Out-Chart -size 275,350 -Title "Sites Created (Last 5 Days)" -label Date -values Count -Template "C:\Program Files\PowerGadgets\Templates\BarChartSoftCreation.pgt" -output "C:\MonitoringFiles\Images\CreationDate.png"

You'll notice I gave an 'output' location with a .PNG filename—this will result in a new PNG image file being created of my chart at the defined location. I'll explain what I do with this file in step 4.

Note: I recommend looking at the PowerGadgets documentation to see all the great things you can do with these charts (including conditional indicators/colors).

Step 3: Readying Central Admin

Since I want these images to show up in Central Admin, I figured I'd create a new picture library to store them in. To explain this briefly in pictures, it went something like this:

Now we're ready to make our script upload to this new directory!

Step 4: Uploading Files

This upload functionality is written as a separate function for two reasons. 1st, it is cleaner (for me) to write code in sections. 2nd, this function can be easily modified for uploading any file or set of files to SharePoint. In this case, it will upload all files in a given directory to the SharePoint directory.

#Upload Functions
function getdestname($filename){"http://sharepointserver:8081/Monitoring%20Images/" + $(split-path -leaf $filename)}
function Upload-FileDirectory($dir){
$wc = new-object System.Net.WebClient
$wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
dir $dir | % { $uploadname=getdestname $_; $wc.UploadFile($uploadname,"PUT", $_.FullName) }
}

At the end of the script (right after we created the 'chart') we just call the Upload function against our 'temporary' image directory:

Upload-FileDirectory "C:\MonitoringFiles\Images"

*I realize this post is starting to feel a bit disjoint, so again, remember the whole script is available in the script library.

Once the pictures are online, drop an "Image Web Part" onto any page in Central Admin (you can create a new 'monitoring dashboard' web part page or use the home page). So now my central admin homepage has a cool looking visual monitor! Now let's use the Windows Task Schedule to overwrite this image with the latest data every night!

Step 5: Scheduling

The windows task scheduler is pretty easy to leverage with PowerShell. Find the task scheduler in Administration Tools and create a new task (I chose to run my daily at 12:01 am) that executes "powershell.exe" with any arguments you might want to pass to your script. Here's a sample (yes, I know this is the vista scheduler!):

Closing Comments

I just wanted to share this 'cool' mesh of SharePoint, PowerShell, and PowerGadgets… hopefully someone can put it to active use! I'd really like to hear about what sort of information you are all interested in tracking—real life, usable examples are always the best!

Comments

Re: Web-based Monitoring (With PowerShell and PowerGadgets)

Very cool!  Would be awesome to also display what the URL is of the site that was created.
at 9/3/2008 1:43 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Comments *


Name (required) *


Attachments
 
 
   
Real Time Web Analytics