Powershell script to get status of VMs in a resource pool

These are just basic powershell scripts. You can use your own logic to enhance this.

cls

#Connect to Vcenter

Connect-VIServer -Server 172.28.xx.xx -user username -Password password

#Take input from the user

$pool = Read-Host "Input the Resource pool name" 

#Select the cluster from which the resource pool need to be selected. '*' represents all. Replace it with the resource pool name to be specific

$rpools = Get-ResourcePool -Location *
$vmpoff = $null 
$vmpon =$null 

#Select each resource pool from the cluster

foreach($rpool in $rpools)
{

#Check if any resource pool name maches the user input

 if($rpool -match $pool)
    {
   $pool = $rpool.Name 
   $vms = Get-VM -Location $rpool
   Foreach($vm in $vms)
      {
      $vmp =$vm.PowerState
      if($vmp -ne "PoweredOn")
         {
         [array]$vmpoff+= $vm
         }
      else 
         {
         [array]$vmpon+= $vm
         }
      }
   }
}
   Write-Host "Powered ON VMs under the Resource pool $($pool) are" $vmpon -Separator `n
   Write-Host "Powered OFF VMs under the Resource pool $($pool) are" $vmpoff -Separator `n
   
exit

Comments

Popular posts from this blog

VMware and Windows Interview Questions: Part 2

VMware and Windows Interview Questions: Part 3

VMware vMotion error at 14%