PowerCLI: Get Running VM’s per VMHost and more


image

This post is inspired by a one-liner from Alan Renouf and question from Jason Boche on Twitter. The original one-liner can be found here: http://www.virtu-al.net

The first one-liner in this post will show the Cluster name, ESX host and the total running VM’s on the host.

Get-VMHost | Select @{N="Cluster";E={Get-Cluster -VMHost $_}},Name, ` 

@{N="NumVM";E={($_ |Get-VM | where {$_.PowerState -eq "PoweredOn"}).Count}}

 image

The second one-liner will return the hostname with the least running VM’s on it:

Get-VMHost | Select @{N="Cluster";E={Get-Cluster -VMHost $_}},Name,  
@{N="NumVM";E={($_ |Get-VM | where {$_.PowerState -eq "PoweredOn"}).Count}} `
| Sort-Object NumVM | Select-Object Name -first 1

 

image

The next “two-liner” will return the VMHost with the least running VM’s and set the VMHost into maintenance mode. This can become handy if you want to patch a VMHost:

$ESXHost = Get-VMHost |Select @{N="Cluster";E={Get-Cluster -VMHost $_}},Name,  
@{N="NumVM";E={($_ |Get-VM |where {$_.PowerState -eq "PoweredOn"}).Count}}`
|Sort-Object NumVM |Select-Object Name -first 1

Get-VMHost $ESXHost.Name |Set-VMHost -State maintenance

The last “two-liner” will return the VMHost with the least running VM’s, Set the host into maintenance mode and shutdown the VMHost:

$ESXHost = Get-VMHost |Select @{N="Cluster";E={Get-Cluster -VMHost $_}},Name,  
@{N="NumVM";E={($_ |Get-VM |where {$_.PowerState -eq "PoweredOn"}).Count}}`
|Sort-Object NumVM |Select-Object Name -first 1

Get-VMHost $ESXHost.Name |Set-VMHost -State maintenance `
|%{Get-View $_.ID} |%{$_.ShutdownHost_Task($TRUE)}
Advertisements

3 thoughts on “PowerCLI: Get Running VM’s per VMHost and more

  1. dan

    I’m trying to write a PowerCLI script to get the virtual machine name, host and datastore on a given cluster. I have seen several examples where you can extract this information from all datastores, but not just a specific. Any suggestions?

    Reply
  2. talltomd

    Working on a script to suspend my VM’s, put hosts into maintenance mode, and then shut down hosts. The problem is that when I put the hosts into maintenance mode they vMotion all the VM’s, which I don’t want. When I investigate the command there is supposed to be an -Evacuate switch which will control that behavior. However, when I try -Evacuate $false I get an error.

    Do you know how to put a host into mainenance mode WITHOUT vMotioning the VMs?

    Thanks,
    Tom

    Reply
    1. afokkema Post author

      You need to set drs to manual before you put the host into maintenance mode. This is also possible with PowerCLI. But remeber, don’t disable drs, ot will remove all the resourcepools.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s