Quick position report, where the vm resides: host, cluster

by Grzegorz Kulikowski

I wanted to have a very quick report that will just get me list of vms and their position in regards of vmhost/cluster. When Virtual Center will go down and you will have to find VC or other vms you might lose precious time. So this report is really nothing fancy, it will just get me VM name, VMh host, Cluster name columns. This is enough for checking where is what then the virtual center had and issue. There are many ways of getting this data, i did it recently like this:
[sourcecode language=”powershell”]
$vmhosts=get-view -ViewType hostsystem -Property name,parent
$clusters=get-view -viewtype ClusterComputeResource -Property name
$vms=get-view -viewtype virtualmachine -property name,runtime.host
$report=$vms|select name,@{n=’HostSystem’;e={$hostshash.($_.Runtime.Host.ToString())}},@{n=’Cluster’;e={$hoststoclusterhash.($_.Runtime.Host.ToString())}} | Sort-Object -Property Cluster

Really nothing fancy about the code, BUT(yeah i left myself a BUT at the end 😉 )
Days : 0
Hours : 0
Minutes : 0
Seconds : 2
Milliseconds : 649
Ticks : 26493602
TotalSeconds : 2.6493602
TotalMilliseconds : 2649.3602

Take a look how much time it took to generate this report. ~2.5 seconds. There were around 2000 objects in this particular $report.
The speed comes from asking only what is needed from get-view + hashtables, so i will not have to use where statements. As you can see there is no ” ? ” or “Where-Object” statement in this tiny script.
Instead of doing get-view -viewtype HostSystem or get-view -viewtype VirtualMachine i use
$vmhosts=get-view -ViewType hostsystem -Property name,parent
$clusters=get-view -viewtype ClusterComputeResource -Property name
$vms=get-view -viewtype virtualmachine -property name,runtime.host
Get-view will get in this case only what i have asked for, therefore it is faster then getting all other properties which are not needed to tell on which host and cluster vm was residing.

I hope you like it, and you can use this method for other things and not only position reports.

Name HostSystem Cluster
--- ---------- -------
VM1 host89.local NTNX_01
VM4 host90.local NTNX_01
VM76 host14.local NTNX_07
VM89 host15.local NTNX_07

In case you want to e-mail it quickly to yourself:
[sourcecode language=”powershell”]
$date=Get-Date -Format ‘dd-M-yyy’

Send-MailMessage -SmtpServer $SMTPserver -to $EmailRCPTs -From ‘VC1@globe.com’ -Subject "VirtualCenter::CurrentPosition at $date" -BodyAsHtml -Body ($report|ConvertTo-Html -PreContent "<h2>Curent vm/host/cluster positions</h2>"|out-string).replace(‘<td>’,'<td style="text-align: center">’)
And it’s sent!
If you want to get it on a daily basis just put the script to your windows task scheduler.

And some references at the end about Powershell hashtables that help me and might help you in getting more comfortable with using them:
http://technet.microsoft.com/en-us/library/ee692803.aspx” title=”http://technet.microsoft.com/en-us/library/ee692803.aspx




You may also like

1 comment

ghousehasan July 7, 2014 - 10:40 am

Thanks for this post. I tested your script and works great. Is there any way to add additional information like vm powerstate and vm uptime to the $report


Leave a Reply

Chinese (Simplified)EnglishFrenchGermanHindiPolishSpanish
Streaming live on Twitch right now.