My life was once filled with fancy server-side scripts, but now I'm just another SharePoint consultant.

Wednesday, January 14, 2009

Non-programmatic Perl to XML Web Part Kludge

Yes! I love ugly processes! What I'm about to describe can, without a doubt, be handled with 5 lines of .NET code. But, what the heck, let's take the scenic route.

What? An attempt to automate an IT business process.

What process? Determining who is at the quota warning level for a file share.

But... Yes, I know it could be done like that... This is how I did it.

Step 1: Acquire Data

Get a report from the Windows Management Instrumentation (WMI) interface for your file server. I do this from a domain joined machine, logged in as a domain admin. If WMI is not enabled on your server, or if a firewall is blocking this query, I can't help you.

Run this from a command-line:

wmic /node:$fileservername diskquota >> quotas.txt

Replace $fileservername with the appropriate server name, or possibly a full-qualified domain name or DNS name.

Let it crunch. On my file server, I get back about 500 entries across several logical disks. The file that's returned (quotas.txt) is a tab-separated values file, a table if you will, with the following 6 fields:

DiskSpaceUsed, Limit, QuotaVolume, Status, User, WarningLimit

Step 2: Crunch Data with Perl

You'll want to take your quotas.txt file and run it through a Perl script that I tweaked up one day. It's a pretty simple script that imports the data, looks for rows with Status = 1, and creates an XML file.

"Status" maps to the WMI diskquota object model:

Case 0 = "OK"
Case 1 = "Warning limit reached"
Case 2 = "Quota exceeded"

(Stolen from Hey, Scripting Guy!)

The script creates an XML file, with nodes setup as such:


(Both "remain" and "limit" are in MB)

Step 3: Upload your new data into an XML web part

Create an XML web part on a site page.
Within the XML web part modification tool, there are two editors, one for XML, and one for XSLT.

Open the XML Editor.
Copy and paste all the data from the XML file that Perl created.
Hit Okay.

Open the XSL Editor.
Copy and paste the following code:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"

<xsl:template match="/">
<table border="1">
<tr bgcolor="#d4cc71">
<xsl:for-each select="danger/quota">
<xsl:sort select="percent" data-type="number"/>
<a><xsl:attribute name="href">http://mysites/Person.aspx?accountname=<xsl:value-of select="person"/></xsl:attribute><xsl:value-of select="person"/></a>
<td align="right"><xsl:value-of select="percent"/></td>
<td align="right"><xsl:value-of select="remain"/> MB</td>
<td align="right"><xsl:value-of select="limit"/> MB</td>


The XSLT formats and tweaks the XML data. It tells the browser to create a table row for each "quota" node and to sort these rows by the "percent" value, which is the percentage of quota used by a specific user.

The XSLT also presents user names as links to their "mysite" pages, specifically the "Person.aspx" view, which does not require a user to have set up their "My Site" site collection. Since the "person" value in the XML maps to the SAM account name of my domain, and because my SharePoint server has an SSP hooked to Active Directory, this works flawlessly.

Step 4: Have fun doing this regularly, since it's not automated

Enough said...

Of course I could automate this kludgy process, but then I'd be missing out on something.

No comments:

Post a Comment

About Me

My photo
Chicago, Illinois, United States