Useful PowerCLI / PowerShell Snippts

Unlike my usual Problem / Solution posts, this post will be updated with helpful PowerCLI pieces I've found during my time picking apart the PowerCLI cmdlet reference!

Get Total Host Info (CPU / Memory)

This snippet retrieves the CPU and memory of every ESXi host in the cluster and displays the total memory and CPU available. The CPUUtil and MEMUtil displays the % of the resources which are in use.

$hosts = get-vmhost;
New-Object -TypeName PSObject -Property @{
TotalMem = ($hosts | Measure-Object "MemoryTotalGB" -Sum).Sum
TotalCPU = ($hosts | Measure-Object "CpuTotalMhz" -Sum).Sum
CPUUtil = ((($hosts | Measure-Object "CpuUsageMhz" -Sum).Sum)/(($hosts | Measure-Object "CpuTotalMhz" -Sum).Sum))*100
MEMUtil = ((($hosts | Measure-Object "MemoryUsageGB" -Sum).Sum)/(($hosts | Measure-Object "MemoryTotalGB" -Sum).Sum))*100

Sample output:

Running VMs without VMware Tools

Any vCenter administrator who has users which can create VMs themselves will know this one. Users never installing VMware tools, always a nightmare! The vSphere client itself can actually do a pretty good job of this, by clicking on VMs and Templates -> The vCenter server itself -> Virtual Machines tab. Then order by State and then VMware Tools Running (This column may need adding manually).

New-VIProperty -Name ToolsVersionStatus -ObjectType VirtualMachine -ValueFromExtensionProperty 'Guest.ToolsVersionStatus' -Force
New-VIProperty -Name ToolsVersion -ObjectType VirtualMachine -ValueFromExtensionProperty '' -Force

These two command snippets create new properties in order to retrieve VMware Tools status. These are required for the following command which then retrieves the status.

get-vm | select Name, ToolsVersion, ToolsVersionStatus | where {$_.ToolsVersionStatus -ne "guestToolsUnmanaged"}

This uses the previous properties which were created to display a list of VMs where the tools are not in the state as defined above. There are a few states which indicate not installed. These can be displayed by simply removing the suffix on the above command:

get-vm | select Name, ToolsVersion, ToolsVersionStatus
Retrieving specific object fields

Most cmdlets provide massive quantities of information and by default this is not displayed by PowerCLI. It is possible to display particular columns which are relevant to the cmdlets by using the select keyword. See below example:

get-vmhost | select Model, NumCPU, CpuTotalMhz, MemoryTotalGB, Version | format-table

The Format-Table command tabulates the results nicely rather than ending up with a big nasty list which is difficult to read.

Adding a NIC to multiple VMs simultaneously

If you suddenly need to connect a whole bunch of VMs to the same network with a new NIC, this can be rather helpful:

get-vm | where {($_.Name -like "SRV_*")}  | New-NetworkAdapter -NetworkName "VM Network" -StartConnected

Where "SRV_*" is a string which matches the name of the VM and "VM Network" is the name of the VMware Port-Group you want to attach those VMs to. (Of course, this port group must exist across the ESXi hosts where your VMs are located).

Enabling VMotion on multiple hosts

New ESXi install on a load of hosts - VMotion isn't enabled! Argh! PowerCLI to the rescue:

get-datacenter | where {$_.Name -eq "DC-1"} | get-vmhost | get-vmhostnetworkadapter | where { $_.PortGroupName -eq "Management Network"} | set-vmhostnetworkadapter -Vmotionenabled $true