Category Archives: powercli

Speed up entering maintenance mode

… via PowerCLI by moving all the powered-off VMs to some other host while DRS handles the powered-on ones:

get-vmhost <maint mode host> | get-vm | where {$_.powerstate -eq "poweredoff"} | move-vm -destination <other host>

Answering VM questions with PowerCLI

Managing Virtual Machine questions with PowerCLI | VMware PowerCLI Blog – VMware Blogs.

Seems like every day there are a number of View VMs that have the question asking if the VM was moved or copied. VMware View can’t do any operations with them until the question is answered, and it won’t answer the question itself so here’s a quick one-liner to just answer them all at once:

Get-VMQuestion | Set-VMQuestion -Option "I copied it" -confirm:$false

This could be scripted to run regularly, but for now I only run it when I see a few pending questions.

Count of powered-on VMs in a cluster

Great couple of one-liners and some thoughts on speeding them up: How many Powered On VMs are running on my host + Speed up your PowerCLI cmdlets! | Boerlowie’s Blog.

I was looking for this to get a quick way to count the number of VMs powered up in a cluster:

get-cluster Cluster01 | get-vm | where {$_.PowerState -eq "PoweredOn"} | measure-object

The article lists some ways to speed up the above by using get-view, but if you are looking for something quick and dirty, this will do the trick.

Quickly add a portgroup to all hosts in a cluster

Sometimes a new VLAN comes along that needs to be added to all hosts. This snippet is probably really obvious to anyone who’s done anything with VMware PowerCLI, but here it is anyway: Quickly add the same new portgroup/vlan to all hosts in a cluster:

get-cluster ProdCluster | get-vmhost | get-virtualswitch -name vSwitch1 | new-virtualportgroup -name Prod-Vlan0123 -vlanid 123

Datastore migration, one VM at a time

Found some good PowerCLI Examples | ForwardOrReverse including the one I was looking for, to move VMs from one datastore to another, one at a time:

# Storage VMotion ALL VMs from one LUN to another (One at a time)
Get-Datastore "Old_Datastore_01" | Get-VM | ForEach-Object {Move-VM -VM $_ -Datastore "New_Datastore_01"}

The nice thing about this is you can open multiple powershell windows and do a few migrations concurrently, if your storage infrastructure is capable of it, without kicking off movement of all VMs at the same time from the source datastore (get-vm | move-vm would cause this to happen).