PowerCLI one-liner to change PortGroup membership on all VMs in a cluster

In my previous post I provided a PowerCLI command to rename PortGroups across multiple hosts in an ESX/ESXi cluster. You would usually do this when making network segment/infrastructure changes. These would be done in two phases:

  1. Rename PortGroups and tag them with the new vlan(s).
  2. Move VMs from the old network segment (PortGroups) into the new one.

The following command would do it:

Get-Cluster "ESX Cluster" | Get-VM |Get-NetworkAdapter | Where {$_.NetworkName -eq "Inside-VLAN" } |Set-NetworkAdapter -NetworkName "DMZ-VLAN" -Confirm:$false

So the command connects to all the VMs running in Cluster ESX Cluster and changes the networks or PortGroups the vNICs are located in from Inside-VLAN to DMZ-VLAN. The last parameter -Confirm:$false suppresses the prompt to confirm the PortGroup changes.

PowerCLI one-liner to rename a PortGroup on all ESX/ESXi hosts in a cluster

It can be a huge hassle to manually rename PortGroups across multiple ESX/ESXi hosts in a cluster. This is where PowerCLI becomes super handy – the one-liner below will take of it:

Get-Datacenter London-DC | Get-Cluster "ESX Cluster" | Get-VMHost | Get-VirtualPortGroup -Name "Inside-VLAN" | Set-VirtualPortGroup -Name "DMZ-VLAN" -vlanid 1002

The above command connects into the London-DC Datacenter object and then to the Cluster ESX Cluster. It will get all the ESX/ESXi servers in the cluster by running Get-VMHost. It then locates the PortGroup called Inside-VLAN and renames it to DMZ-VLAN and tags it with vlan ID 1002.

If you have upwards of 5 ESX/ESXi hosts it will save you a lot of time and effort.

Cluster validation error – the wrong diskette is in the drive

While configuring a new MSCS Cluster in VMware ESXi 5.1 I encountered the following error when running the Cluster Validation Tool:

The wrong diskette is in the drive
Insert %2 (Volume Serial Number: %3) into drive %1.

I was very surprised to see an error indicating that the problem was with a diskette. Obviously this wasn’t right, my suspicion was that it was storage related so I requested a SAN Engineer to take a closer look. The cursory checks were made such as ensuring PR (Persistent Reservations) was set on the LUN but no glaringly obvious causes were identified.

After trying a few things (and failing) I ‘cleaned’ the clustered disks, which fixed the problem. This involved converting the disks from GPT to MBR and then back again. The following steps  were performed:

  1. Open Disk Management by running diskmgmt.msc from a command prompt
  2. The disk contained a volume, so right-clicked on the volume (within the disk) and then clicked Delete Volume.
  3. Right-clicked the GPT disk and then clicked Convert to MBR disk.
  4. Once the disk had converted to MBR I right-clicked on it and then clicked Convert to GPT disk.

I then re-ran the Cluster Validation Test and it passed successfully!

Change a GUID Partition Table Disk into a Master Boot Record Disk
Change a Master Boot Record Disk into a GUID Partition Table Disk

PowerCLI one-liner to add a PortGroup to all ESX/ESXi hosts in a cluster

A useful one-liner to add a PortGroup to all ESX/ESXi hosts in a cluster:

Get-Cluster "ESX Cluster" | Get-VMHost | Get-VirtualSwitch -Name "vSwitch0" | New-VirtualPortGroup -Name "DMZ-VLAN" -vlanid 1002

It adds a PortGroup called DMZ-VLAN to a VirtualSwitch called vSwitch0 which is tagged with vlan 1002 on all ESX/ESXI hosts in the cluster called ESX Cluster.

PowerCLI command to list all VMs in a cluster

A quick PowerCLI command to list all VMs in a cluster:

Get-Cluster "Cluster Name" | Get-VM

To sort by name run:

Get-Cluster "Cluster Name" | Get-VM | Sort Name

To export to a csv file run:

Get-Cluster "Cluster Name" | Get-VM | Export-CSV C:\ListVMsInCluster.csv

The commands above are very simple and because I am so forgetful I need them somewhere I can quickly find them.