New Scripts – Bandwidth Report and Google Charts
Got a couple of new scripts I recently wrote, here’s some quick thoughts on them.
This script was originally inspired by this script, and I first wanted to talk to the original author to make sure he was cool with me porting to PowerShell, and to find out how he did a couple of things. He gave me his full blessing and it was time to begin working on the script.
The actual measuring of time for the Mbps calculation would be really easy as I would just wrap a Copy-Item cmdlet in a Measure-Command object. I’ve done that kind of testing many times for performance testing so it would be great to actually use it in a production script! Now I needed a file of about 200mb. Turns out this is a bit more difficult to find than I anticipated though I did eventually find something on Microsoft that as about 241mb. The problem here was I wanted it to be easy to find so anyone could do it, maybe even have the script automatically download it. But software links on the Internet are notorious for constantly changing and I began to realize this approach wouldn’t work. So how to create a dummy file of the right size? With a little Google-fu I discovered a Windows utility called FSUTIL. Perfect! I could now create a dummy file of any size and it’s fast.
I couldn’t quite decipher how to calculate Mbps from the Python script so I did another Google search and found this, but the numbers I was getting just didn’t seem right. I am on a 54mbps wireless network and I was seeing transfer speeds of 150mbs+ in speed?! So I consulted a good networking friend of mine–he hasn’t posted to his blog in a long time, maybe we can get him a few hits and he’ll get back into it–who lives and breaths this stuff and he thought the calculation was sound, except that instead of using 1,000,000 to do the change from bps to Mbps I should use 1,048,576 (which is 1024 x 1024). This made sense since it was a Mega-Byte per second. But didn’t explain why my number was coming up wonky. Until I noticed I was converting my dummy file to bits TWICE (Total Size * 8 * 8). Oops! Fix that and suddenly the numbers start making sense. Here’s the calculation I ended up using:
$Mbps = (($DummyFileSizeInBytes * 8) / $CopyTime.TotalSeconds) / 1048576
After this the rest of the script came together pretty easily. Unfortunately the script was trying so hard to be all things to all people that I began to wonder if I was doing it right.
We need to spend some time apart…
One major tenet of PowerShell is reusability. This is why there’s such a heavy focus on using objects, so you can use your script with any cmdlet, or even other people’s scripts! That’s why ConvertTo-AdvHTML even works! I had a lot of extra code in the script that allowed the output to be just an object–as well as text, HTML and CSV!–but the script was getting big and awkward at this point. Time to split it in two! Interesting, a couple of days after making this decision the conversation came up at the Spiceworks forum.
This really simplified the first script. Once I could focus on just delivering an object I was able to streamline and get the script written quickly. There’s nothing really ground breaking about the script itself so I won’t go into details about how it works, but here’s the final code:
So the much more interesting script (at least to me, I guess I’m a report wonk!) was the HTML to follow. First I needed to save the data for historical purposes. The obvious choice would be to use Export-CSV for this, but I don’t like how CSV flattens all of the fields to a string, forcing you to convert everything back to their native variable types when you read the data back in. To avoid this I like use Export-Clixml, which saves the data in XML format and retains all of the variable types and properties. So easy to just Export/Import the objects! After that it’s a matter of adding some Google charts, and using my ConvertTo-AdvHTML function and you end up with:
Speaking of Charts
From there I pipe the object into the New-GoogleChart function with the properties I want to use:
And here’s what it looks like:
If you ever end up using this function, please let me know! I want to see what you did with it!