Guest Post By Darren White
Today myself and Dave where working on a customer site when the question of HyperV statistics came up, it would be good to be able to see what free memory was available on the host machine and also what memory was being used by the guest OS’s, now as has been pointed out in previous posts Dave is no expert with PowerShell but very much a newbie, whereas I am a past master (I do my best). So after searching codeplex I found the PowerShell Management Library for Hyper-V.
After downloading and extracting please remember to unblock the zip file as illustrated below or you may encounter errors when trying to install and run the commands.
Once installed the obvious first step was to see the output of the commands, this proved to be very extensive and perfect for the job. The command we were most interested in was Get-VMSummary as illustrated below.
As you can see the output is in list format for each item and this makes it hard to get an overview of data so the next step was to pipe it into Format-Table; Get-VMSummary | Format-Table. As illustrated below the formatted table makes it easier to read.
As you can see there is a lot of information presented that we do not need, so by using the format-table command we can specify the columns we wish to output to the screen. The main information we needed was:-
Hostname (Host), Virtual Machine Name (VMElementName), Installed Operating System (GuestOS), Number of Allocated CPU’s (CPUCount), Allocated Memory (MemoryUsage), Current State i.e. Running/Stopped (EnabledState)
Get-VMSummary | Format-Table Host, VMElementName, GuestOS, CPUCount, MemoryUsage, EnabledState
The next step was to output the information to a CSV file; this was achieved by replacing Format-Table with Select-Object and then piping this into the Export-Csv command. We also used the Sort-Object command to order the output by virtual machine name.
Get-VMSummary | Sort-Object VMElementName | Select-Object Host, VMElementName, GuestOS, CPUCount, MemoryUsage, EnabledState | Export-Csv VMInfo.csv -NoTypeInformation
The next step was to get the information from all the HyperV servers in the current domain this was achieved by using the Get-VMHost command as can be seen in the illustration below.
To continue writing our PowerShell script we included the parameter –server for the Get-VMSummary command. We passed this parameter from the Get-VMHost inside a foreach loop.
Get-VMHost | Sort-Object | foreach {
Get-VMSummary -server $_ | Sort-Object Name | Select-Object Host, VMElementName, GuestOS, CPUCount, MemoryUsage, EnabledState | Export-Csv “$_ VMInfo.csv” –NoTypeInformation
}
This will create a new CSV file for each hyperV server with the virtual machine information we have asked for.
The next step we needed to achieve was to merge the individual CSV files into one master CSV file containing the desired information on all the virtual machines in your domain. To do this we need to store to output in an array for each HyperV server instead of outputting to the CSV file. Once the array is populated we output this to the master CSV file.
$content = @();
Get-VMHost | Sort-Object | foreach {
$content += Get-VMSummary -server $_ | Sort-Object Name | Select-Object Host, VMElementName, GuestOS, CPUCount, MemoryUsage, EnabledState
}
$content | Export-Csv VMInfo.csv –NoTypeInformation
This script we saved as a PS1 file and then ran from within PowerShell, the CSV output you can see below.
This gives a great overview of all the virtual machines you have running on your HyperV server and will be a great aid when it comes to balancing the load across your HyperV servers.
A trick we used to balance the server load was, once we opened our CSV file in Excel you can use the function sumif to create the following formula, =SUMIF($A:$A,H1,$E:$E).
In our CSV column A we have the HyperV Hostname and Column E is the Virtual Machine Memory allocation. Cell H1 is next to the formula containing a HyperV hostname. We then copied this formula to the cells below for each HyperV (i.e. H2 will contain the hostname for the second HyperV and so on)
This will show the amount of Ram used on each HyperV server by the virtual machines and we hope it helps with balancing your HyperV implementation. As you can see from the illustration above our HyperV server will need balancing but that is the point of the post so we can now live migrate our virtual Servers and rerun the script until we are happy with the spread on across all of our HyperV host servers.
I hope you enjoyed this post and any questions please submit via comments on the blog or by going to http://twitter.com/whitefern and asking me there.
Darren
You may also be interested in reading:
- HyperV Cluster Setup Part 4 This is the fourth part of the HyperV cluster setup...
- Saving Money with Virtualisation Using HyperV R2 As i said in a previous post in our preparation...
- HyperV Cluster Setup Part 5 This is the fifth and final part of the HyperV...
- HyperV Licencing of Guest Operating Systems As I have said I previous posts myself and Alan...
- Free HyperV Monitoring Tool Myself and Alan Richards have so far given 3 SharePoint...
- Fully Virtualised Environments in HyperV. Theory meets reality head on! You may be aware of a constant debate that goes...














2 pings
Tweets that mention Chris and Daves SharePoint and Tech Blog » Getting HyperV Statistics Using PowerShell -- Topsy.com
August 19, 2010 at 11:11 pm (UTC 1) Link to this comment
[...] This post was mentioned on Twitter by Planet SharePoint, Dave Coleman. Dave Coleman said: Guest post by @Whitefern – Getting HyperV Statistics Using PowerShell http://tinyurl.com/3yrsopq #HyperV #Server2008R2 [...]
Benefits of Hyper-V Virtual Dedicated Server | Dedicated Servers
September 12, 2010 at 5:53 pm (UTC 1) Link to this comment
[...] Chris and Daves SharePoint and Tech Blog » Getting HyperV … [...]