My Go-To PowerCLI One-Liner for Virtualization Pros

PowerCLI One-Liners for Your Virtualization Needs

As a virtualization administrator, I have been working with PowerCLI to automate various tasks and streamline my workflow. Over the past few months, I have come up with several one-liners that I would like to share with you. These one-liners are not fancy, but they have proven to be useful in my day-to-day work. Here are some of the one-liners that I have found helpful:

1. 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”

This one-liner changes the multipathing policy for all hosts and datastores in the PROD cluster to round-robin. This is useful when you want to ensure that all hosts and datastores are treated equally, and that no single host or datastore is favored over others.

2. Getting a list of all VMs in a cluster and the datastore in which the VMs reside:

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

This one-liner retrieves a list of all VMs in the current cluster and their corresponding datastores. You can use this information to check which datastores are being used by which VMs, and make decisions about datastore usage and capacity planning.

3. Getting 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 $_}}

This one-liner retrieves a list of all VMs in the current cluster, along with their network adapters, mac addresses, and connected port groups. This information can be useful for troubleshooting network issues, identifying which VMs are using which port groups, and planning for network upgrades.

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

This command performs a vMotion of a VM between hosts without using shared storage. This can be useful when you want to move a VM from one host to another, but you don’t have access to the source or destination host’s storage.

5. Enable SSH on all hosts:

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

This one-liner enables SSH on all hosts in the current cluster. This can be useful when you want to allow remote access to your ESXi hosts, but you don’t want to enable SSH for all users by default.

6. Checking on which hosts SSH is enabled:

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

This one-liner retrieves a list of hosts that have SSH enabled. This can be useful when you want to check which hosts are allowing remote access, and make decisions about security and access control.

7. Getting a list of hosts and the number of VMs running on these hosts:

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

This one-liner retrieves a list of hosts and the number of VMs running on these hosts. This information can be useful for capacity planning, identifying which hosts are underutilized or overutilized, and making decisions about host deployment and scaling.

I hope you find these one-liners helpful in your day-to-day work with PowerCLI. If you’re looking for more advanced PowerCLI stuff, I recommend checking out the blogs of Alan Renouf and Luc Dekens. They have a wealth of knowledge and experience with PowerCLI, and their blogs are full of useful tips and tricks.