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

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:

$vmhosts=get-view -ViewType hostsystem -Property name,parent
$clusters=get-view -viewtype ClusterComputeResource -Property name
$vms=get-view -viewtype virtualmachine -property name,runtime.host
$hostshash=@{}
$clustershash=@{}
$hoststoclusterhash=@{}
$vmhosts|%{$hostshash.Add($_.moref.toString(),$_.name)}
$clusters|%{$clustershash.Add($_.moref.toString(),$_.name)}
$vmhosts|%{$hoststoclusterhash.add($_.moref.toString(),$clustershash.($_.Parent.ToString()))}
$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:

$SMTPserver="mysmtp.server.local"
[String[]]$EmailRCPTs='email1@globe.com','email3@globe.com'
$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.
Enjoy!

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

http://blogs.technet.com/b/heyscriptingguy/archive/2013/09/15/powertip-use-powershell-to-search-a-hash-table.aspx

http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/04/learn-the-basics-of-powershell-hash-tables.aspx

http://blogs.technet.com/b/heyscriptingguy/archive/2011/10/16/dealing-with-powershell-hash-table-quirks.aspx

Advertisements