My most frequently used PowerCLI One-liner

Over the last months I wrote different PowerCLI One-liners who I want to share. Nothing fancy and one or two are ugly. But they worked for me. :)

Changing the multipathing policy for all hosts and datastores in a cluster

Get-Cluster PROD | Get-VMhost | Get-scsiLun -CanonicalName “naa.60030*”| Set-ScsiLun -MultipathPolicy "roundrobin"

Get a list of all VMs in a cluster and the datastore in which the VMs resides

Get-Cluster | Get-VM | select name, @{N="Datastore";E={Get-Datastore -VM $_}} | sort name

Get a list of all VMs, their mac-address and the connected port groups

Get-VM | Select Name, @{N="Network Adapter";E={$\_.NetworkAdapters| foreach-object {$_.Type}}}, @{N="MacAddress";E={$_.NetworkAdapters| ForEach-Object {$_.MacAddress}}}, @{N="PortGroup";E={Get-VirtualPortGroup -VM $_}}

vMotion of a VM between hosts without a shared storage (not really a One-liner…)

Move-VM -Destination esx-lab-01.testlab.site -Datastore local_ESX_LAB_01 -VM TSTCLN02

Enable SSH on all hosts

Get-VMHost | Foreach {Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )}

Check on which hosts SSH is enabled

Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH" } |select VMHost, Label, Running

Get a list of hosts and the numer of VM that are running on these hosts

Get-VMHost | Sort-Object Name | Select Name,@{N="VM";E={ if ($_.ExtensionData.Vm -ne $null) { $_.ExtensionData.Vm.Count } else {0}}}

If you’re searching for more advanced PowerCLI stuff visit the blogs of Alan Renouf and Luc Dekens.