Charts are awesome – they can help make sense of endless reams of text and data and they generally look pretty. So my question to myself was: “How do I get useful data I generate using PowerCLI into a nice, neat little chart?” I had a quick google and found a couple of different solutions. The one that stood out as being the easiest to start off with for me was to use the “Microsoft Chart Controls for Microsoft .NET Framework 3.5”
I read a few blog posts around detailing how to create these custom .NET charts in PowerShell, but this tends to be quite a tedious process – akin to creating a Windows Forms GUI in PowerShell manually – basically a complete pain. The blog posts I read definitely helped me understand how to create charts and soon I was able to generate some pretty cool charts based off data from PowerCLI (or PowerShell) data. I wanted to ultimately automate the creation of Charts for my PowerCLI and PowerShell scripts, so I decided to create myself a Function that could be used anywhere to generate a Bar or Pie Chart on the fly.
httpv://youtube.com/watch?v=EvQ2znWr0lk
Enter Create-Chart. This is the Function I have made that accepts a bunch of Parameters to create a custom Chart and outputs this to a .PNG file. The data needs to be fed in to the function via a Hash Table (this could be changed) so I also created a “helper” Function called Create-HashTableย which also does the work of generating a hash table for use with the Create-Chart Function. You could of course also just feed the Create-Chart function with a manually created HashTable too – this is useful to know because my Create-HashTable function is fairly basic and is not too flexible. Here are a couple of examples of Charts I created using these Functions:
Download the two functions below to give them a try! Please do suggest any improvements – my parameter handling on the Create-Chart script needs work – they are not specified as mandatory, although all parameters are mandatory – I couldn’t get the Function to work correctly when I did make them mandatory. The Create-HashTable function could also be improved in that at the moment you can only specify Cmdlets for the “Cmdlet” parameter that do not have any double quotation marks inside the cmdlet or any $_ variables. This is because of the way I am using the Invoke-Expression cmdlet inside the function. A simple cmdlet parameter such as “Get-VM | Select Name, MemoryMB” would work just fine for example. Remember that the Create-Chart function needs to be fed with a Hash Table. This could be generated yourself, or by using the Create-HashTable function below. Here are the downloads:
Don’t forget the Microsoft Chart Controls – a requirement to run these functions
Download the functions here:
https://www.shogan.co.uk/wp-content/uploads/Create-Chart.zip
https://www.shogan.co.uk/wp-content/uploads/Create-HashTable.zip
Once you have the Functions loaded, here are some examples to show you how to use them:
Pie Chart of VMs and their MemoryMB setting:
Create-Chart -ChartType Pie -ChartTitle "Sean's Awesome VM Chart" -FileName seanchart3 -XAxisName "VMs" -YAxisName "MemoryMB" -ChartWidth 750 -ChartHeight 650 -DataHashTable (Create-HashTable -Cmdlet "Get-VM" -NameProperty Name -ValueProperty MemoryMB)
Bar Chart of ESXi Hosts and their Memory Usage (MB) values:
Create-Chart -ChartType Bar -ChartTitle "Sean's Awesome Host Chart" -FileName seanchart4 -XAxisName "VM Hosts" -YAxisName "Memory Usage (MB)" -ChartWidth 750 -ChartHeight 650 -DataHashTable (Create-HashTable -Cmdlet "Get-VMHost" -NameProperty Name -ValueProperty MemoryUsageMB)
Use your own Hash Table to input the data:
Create-Chart -ChartType Bar -ChartTitle "Custom Chart" -FileName seanchart5 -XAxisName "My Objects" -YAxisName "My Object Values" -ChartWidth 750 -ChartHeight 650 -DataHashTable $HashTable
So there you have it – a fairly easy way to Chart the data you can get from your PowerCLI or PowerShell cmdlets! I wrote these Functions as part of a larger report that I am working on for another soon to come blog post! As I mentioned above, there is plenty of room for improvement – so if you do make any improvements or changes, please be sure to post them in the comments section.
@Marcus,
Sorry about that – an old download plugin that is no longer enabled. I’m adding the direct download links to the post now.
PS, are you the Marcus that replied on this post back in 2013?!
Cheers
Hi Sean, I couldn’t find download links of the function files.
I have downloaded MS chart file from download link but not able to get the report getting this error and how cn i show this graph output in HTML ouput in email.
Error:
Create-HashTable : The term ‘Create-HashTable’ is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path
is correct and try again.
At line:1 char:176
+ … rtWidth 750 -ChartHeight 650 -DataHashTable (Create-HashTable -Cmdlet …
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Create-HashTable:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Thanks for reply.
I am looking for rounded decimal value in oneliner command for creating graphs.
Create-Hash-Table -Cmdlet “Get-VM” -NameProperty Name -ValueProperty ProvisionedSpaceGB
Plz suggest for this.
Hi Ravi,
Thanks!
Have you tried using the system math functions?
If you have your ProvisionedSpaceGB value in a variable you can do this:
$roundedResult = [math]::round($myValue,2)
This will round that value to two decimal places.
Cheers,
Sean
Hi Sean, Awesome article.
I have to achieve ProvisionedSpaceGB of VMs in rounded decimal. As of now i am able to display the values on graph but not in rounded decimal.
Please help.
@Marcus
Hi Marcus,
I had a quick play around with this and it seemed OK for me.
First I created my hashtable of hosts like so:
$hashex = Create-HashTable -Cmdlet “Get-VMHost | Sort Name” -NameProperty Name -ValueProperty NumCPU
(Note that I pipe the output to “Sort Name” so that it sorts alphabetically. Did you do this too?)
Then I created a bar chart with this hash table like so:
Create-Chart -ChartType Bar -ChartTitle “Custom Chart” -FileName seanchart5 -XAxisName “My Objects” -YAxisName “My Object Values” -ChartWidth 750
-ChartHeight 650 -DataHashTable $hashex
Viewing the chart, it is is alphabetical order – see the chart here: http://dl.dropbox.com/u/450727/shogantech/seanchart5.png
If you have already been piping your PowerCLI cmdlet output to “Sort Name”, then send me along the commands you are using to reproduce the issue and I’ll take a look!
Thanks,
Sean
Hi Marcus,
Let me see if I can replicate this myself and figure out a solution for you. Apologies for the delayed reply, but I’ll take a look this evening and get back to you. Not sure why it would be doing this at first glance, but I’m sure we can figure it out.
Cheers!
Sean
Hi Sean,
I’m not sure if I’m doing something wrong but I tried everything to solve this problem and still nothing: The Hashtable I create in my script does not appear in order when I use the Create-Chart cmdlet with it. For example, I have a hashtable named named $VMHosts that contains all ESXi hosts in alphabetical order, I can even print it out to confirm that it is in order. But when I use the Create-Chart cmdlet, the ESXi hosts don’t appear in alphabetical order on my bar chart, any idea why?
Thanks a lot for your great work!
Hi Aaron!
Well, to start off Get-Datastore doesn’t have a default property called “UsedGB”. So what do we do? We need to create this property ourselves using the New-VIProperty cmdlet. Type the following in:
New-VIProperty -Name UsedGB -ObjectType Datastore -Value {“{0:f1}” -f ($Args[0].CapacityGB – $Args[0].FreeSpaceGB) }
(Put the above in your PowerShell profile if you want it to always be active in all your PowerCLI sessions).
Then run the following Create-Chart / Create-HashTable combination as per my blog post above:
Create-Chart -ChartType Bar -ChartTitle “DS Chart” -FileName chart1 -XAxisName “Datastores” -YAxisName “Used GB” -ChartWidth 750 -ChartHeight 650 -DataHashTable (Create-HashTable -Cmdlet “Get-Datastore” -NameProperty Name -ValueProperty UsedGB)
That should do the trick and you should get your info into a chart ๐ Remember that there was a bug with PowerCLI version 5.0 where FreeSpaceGB and CapacityGB were mixed up, so if you are using this version, upgrade to 5.1 to avoid getting odd results (as to create our custom property called UsedGB we are subtracting FreeSpaceGB from the CapacityGB figures.
Let me know if that helps ๐
Cheers,
Sean
THanks a ton!,
Question. Any way to use this exact same script to display Datastore usage? I changed the hash table from :
(Create-HashTable -Cmdlet “Get-VMHost” -NameProperty Name -ValueProperty MemoryUsageMB)
to:
(Create-HashTable -Cmdlet “Get-Datastore” -NameProperty Name -ValueProperty UsedGB)
but it wont display the bar graph. It pulls the datastore names fine, just wont display the values on the graph
Hi Robin,
Thanks for the comments and for spotting the typo! I have corrected that now ๐ Your modification sounds good – currently the script will just default to save the chart to whichever path you are currently changed to in PowerShell.
Sean
Amazing stuff! Just going to have a go at implementing it now with a few modifications (like an export path parameter for the image) :). Noticed a small typo on your blog entry – the second example code is a bar chart and not a pie chart. Thanks again!