Thursday, May 31, 2012

Feather duster PowerCLI scripts!

I've been cleaning out script repository and found a file full of PowerCLI scripts I found useful. Hopefully they'll be of use to you too.

What is my consolidation ratio? (number of VMs divided by number of hosts)
(Get-VM | Measure-Object | Measure-Object -Sum Count).sum/(Get-VMHost | Measure-Object | Measure-Object -Sum Count).sum

What is my consolidation ratio rounded to two decimal places?
[Math]::Round((Get-VM | Measure-Object | Measure-Object -Sum Count).sum/(Get-VMHost | Measure-Object | Measure-Object -Sum Count).sum,2)

How many hosts in my cluster aren't ESX 5.0?
Get-Cluster myCluster | Get-VMHost | Where-Object {$_.Version -ne '5.0.0'} | Select-Object Name

What's versions of ESX do I have in my cluster, and how many?
Get-Cluster myCluster | Get-VMHost | Group-Object Version

Count Name     Group
----- ----     -----
    2 5.0.0    {esx01.test.lab, esx02.test.lab....
    1 4.1.0    {esx03.test.lab....

Which VMs in this cluster have SCSI bus sharing set to physical, and what hosts are they on?
Get-Cluster myCluster | Get-VMHost | Get-VM | Get-ScsiController | Where-Object {$_.BusSharingMode -eq 'Physical'} | Select {$_.Parent.Name}, {$_.Parent.Host}, BusSharingMode | Sort {$_.Parent.Host}

$_.Parent.Name  $_.Parent.Host  BusSharingMode
--------------  --------------  --------------
mySQLServer     esx01.test.lab  Physical

How many VMs are on each of my ESX hosts?
Get-VMHost | ForEach-Object {$_.Name; Get-VM -Location $_ | Measure-Object | Select-Object count}

Create a new VMFS datastore on a SAN LUN given a disk's NAA
New-Datastore -Vmfs -VMHost (Get-VMHost esx01.test.lab) -Name myNewDatastore -Path "naa.10000038275027693935893472859354"

Rename a datastore
Get-Datastore BurgerHut | Set-Datastore -Name HungryBurger

Total space consumed by VMs in TB
((Get-VM | Get-HardDisk | Measure-Object -Property CapacityKB -Sum).sum/(1024*1024*1024)).ToString() + " terabytes consumed"
519128.167283058 gigabytes consumed

Total number of datastores that are X KB in size
Get-Datastore | Where-Object {$_.CapacityMB -eq 500} | Measure-Object | Select-Object Count

Total number of VMs
Get-VM | Measure-Object | Select-Object Count

No comments:

Post a Comment