Developmentish

July 30, 2011

Something Cool: BI on the fly with PowerShell

Filed under: Cool Stuff, Powershell — Martyn Gough @ 12:17 am

So far I’ve only done a couple of silly little quick tips… So lets try something a bit more complex…

As a serious fan of PowerShell the more I can do with it, the better… So that got me thinking, why not create some sort of chart on the fly based on system information?
First problem I encountered was picking a nice simple method to use, I opted to use the google charts api as it’s a more pervasive technology than something like SQL server.

So lets get cracking, I just kept the it “powershell light” and simply outputted my top 10 processes based on CPU usage.

            
function Get-GChart ($url) {             
    $filename = $home + "\chart.png"             
    $webClient = new-object System.Net.WebClient             
    $Webclient.DownloadFile($url, $filename)             
    Invoke-Item $filename             
}             
             
function Set-Encode ($valueArray, $labelArray, $size, [switch] $chart3D) {             
    $simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'             
    if ($chart3D) {$chartType = "p3"} else {$chartType ="p"}             
    $total = 0             
    foreach ($value in $valueArray) {             
        $total = $total + $value             
    }             
    for ($i = 0;$i -lt $valueArray.length;$i++) {             
        $relativeValue = ($valueArray[$i] / $total)*62             
        $relativeValue = [math]::round($relativeValue)             
        $encodingValue = $simpleEncoding[$relativeValue]             
        $chartData = $chartData + "" + $encodingValue             
    }                 
    $chartLabel = [string]::join("|",$labelArray)             
    Write-Output "http://chart.apis.google.com/chart?chtt=Running Process CPU Usage&cht=$chartType&chd=s:$chartdata&chs=$size&chl=$chartLabel"             
}             
             
function Get-ProcessArray() {             
    $ListOfProcs = Get-Process | Sort-Object CPU -desc | Select-Object CPU, ProcessName -First 10             
    $ListOfProcs | ForEach-Object {             
        $ProcName = $ProcName + "," + $_.ProcessName             
        $ProcUsage = $ProcUsage + "," + $_.CPU             
    }             
    Write-Output (($ProcName.trimStart(",")).split(","), ($ProcUsage.trimStart(",")).split(","))             
}             
             
$data = Get-ProcessArray             
$url = Set-Encode $data[1] $data[0] "700x350" -chart3D             
Get-GChart $url             

Ok… what the hell was that?
you’ll notice the 3 functions then 3 lines of code at the bottom… We’ll work from the function calls then explain each function…

I assign $data to the results of the Get-ProcessArray.
This function uses get-process to sort the running processes by CPU then select the CPU and name of the process. We then pipe these 2 values into 2 lists, one for cpu one for name. After this we output these lists as an array.

Now the complicated bit we push the array into the next function Set-Encode.
This function first picks the chart type, adds up all the totals in the value list. We then do some calculation to work out the pie segments then encode it into the correct format for the google charts api to understand.
We then “Join” our label list meaning we turn it into a pipe separated string. Now we have everything we need to build a url for google, so we write that back to the main code.

And finally, we need to get the chart… We simply pass out url value into the Get-GChart function.
This function creates creates a filepath to download to in this case I used my PowerShell default path of $home. We then call the .Net class System.Net.WebClient to perform our internet bidding… Using this we can call .DownloadFile passing in the url to download from and the path to download it to…
Aaaaaaaaaaand finally, Invoke-Item to open our newly downloaded image.

Like all powershell scripts, this was a hell of a lot easier to write than to explain.

Laters!

Blog at WordPress.com.

Design a site like this with WordPress.com
Get started