Friday, July 20, 2012

Powershell to get VM, ESX, Datastores and... computer rooms

Having decided to make a Powershell map of the vSphere virtual machines, ESX hosts, datastores and computer rooms of my company, I wrote the following script which aims to generating a grid view (through the use of the nice Out-GridView cmdlet) showing those information in a easily human-readable manner. Of course in my script I had to make a few concession: for instance I had no dynamic information about the computer room inside which every ESX is found, so I have hard-coded the list of our computer rooms in a Switch statement which you have to adapt to your infrastructure:
switch -wildcard ($ 
        "ESXhost1*" {"Site-1-Computer-room1"} 
        "ESXhost2*" {"Site-1-Computer-room1"} 
        "ESXhost3*" {"Site-1-Computer-room2"} 
        "ESXhost4*" {"Site-2-Computer-room1"} 
        "ESXhost5*" {"Site-3-Computer-room1"} 
        "ESXhost6*" {"Site-3-Computer-room2"} 
        "ESXhost7*" {"Site-3-Computer-room3"} 
        default {"Unknown computer room"}
For the rest all the operations are automatic. I first retrieve all the VMs on my vSphere servers (with Get-VM), then for each VM I go retrieve 8 properties:

 - the VM name
 - the VMhost
 - the computer room
 - the # of GB effectively used by the VM
 - the # of GB provisioned to the VM
 - the name of the datastore containing the virtual machine disks (vmdk)
 - the size in GB of the datastore
 - the # of free GB of the datastore

To get the final report done I had to use a few Powershell goodies such as  'Nested Expressions' and mathematical Round function:
@{label="Datastore size";expression={(Get-Datastore -VM $_ | 
select @{label="Datastore size";expression={[Math]::Round($_.capacityMB/1kb,2)}}).'Datastore size'}}
Probably this is not the bast way to do it, that's why I am asking you to comment and suggest improvements to my script if you feel like.

Once the grid appears on the screen you can perform any kind of filtering using the methods allowed by this kind of object.

Heres' the script which you can copy and save as 'vm_and_datastores.ps1'.
Generates a table with VM allocation size and datastore utilization.

Generates a table reporting detailed information about VMs size, provisioned and used space as well as datastore information.

Mandatory parameters is vc.
No optional parameters.

Specifiy the name of the vSphere server.

vm_and_datastores -vc virtualcentername

Author: Carlo
Date: 7/20/2012


$vc = $(Read-Host 'Enter vSphere server name'))

Add-PSSnapIn VMware.VimAutomation.Core
Connect-VIServer $vc

Get-VM | sort name | select name,vmhost,@{label="Computer room";expression={switch -wildcard ($ 
        "ESXhost1*" {"Site-1-Computer-room1"} 
        "ESXhost2*" {"Site-1-Computer-room1"} 
        "ESXhost3*" {"Site-1-Computer-room2"} 
        "ESXhost4*" {"Site-2-Computer-room1"} 
        "ESXhost5*" {"Site-3-Computer-room1"} 
        "ESXhost6*" {"Site-3-Computer-room2"} 
        "ESXhost7*" {"Site-3-Computer-room3"} 
        default {"Unknown computer room"}
@{label="Used GB";expression={[Math]::Round($_.usedspacegb,2)}},
@{label="Provisioned GB";expression={[Math]::Round($_.provisionedspaceGB,2)}},
@{label="Datastore name";expression={(Get-Datastore -VM $_ | select name).name}}, 
@{label="Datastore size";expression={(Get-Datastore -VM $_ | 
select @{label="Datastore size";expression={[Math]::Round($_.capacityMB/1kb,2)}}).'Datastore size'}},
@{label="Datastore free GB";expression={(Get-Datastore -VM $_ | 
select @{label="Datastore free GB";expression={[Math]::Round($_.freespaceMB/1kb,2)}}).'Datastore free GB'}} |
Out-GridView -Title "VM & Datastores by"
As I said, I am open to comments/questions/suggestions/critics/whatever. 

1 comment:

  1. This is great, Thanks!

    Is there a way that I can provide an input of VMs through a text file. I mean I dont want to run it against the vcenter and ALL VMs in it.

    For my QA, I need to check a few VMs as to what ESX host and datastore do they reside, If the VMtools is uptodate and if there are any current alarms for those VMs. How can I do this?

    All I may need is a script file which calls a text file that contains the list (which I will update time to time with different sets of VMs). Your help will be appreciated.

    Thanks again!


Related Posts Plugin for WordPress, Blogger...