Author Archives: chouse

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).

Recreating a missing virtual machine disk (VMDK) descriptor file

For those days where you feel like pulling out your hair, VMware offers these lovely articles:

Had a VM the other day with a snapshot. Needed to make a new snapshot, but it refused. Powered off the VM to make the snapshot (failed) and then it wouldn’t power back on. Looked in its folder and found the -flat and -delta VMDKs (data intact), but no descriptor files.
Luckily the snapshot chain was not long and it was easy to recreate the descriptor files, even though vmkfstools did not like the ‘pvscsi’ controller option, so I used lsi and changed it to pvscsi it in the descriptor file.

Copy vSwitch portgroups to a new host

Based on: PowerCLI: Easy vSwitch & PortGroup Setup | Virtu-Al.Net.

Here’s a great one-liner to copy all the portgroups from one host’s vswitch to a new host’s vswitch (example below copies oldhost’s portgroups on vSwitch1 to newhost’s portgroups on vSwitch2, keeping VLAN IDs intact):


$oldhost = get-vmhost -name oldhost.local.dom
$newhost = get-vmhost -name newhost.local.dom

$oldhost | get-virtualswitch -name vSwitch1 | get-virtualportgroup | foreach { $newportgroup = $newhost | get-virtualswitch -name vSwitch2 | new-virtualportgroup -name $_.Name -VLanID $_.VLanID }