Using a vSphere GUI to manage your environment is sufficient for many tasks, but there are times when you need to perform certain advanced tasks where the GUI fails at that and you need PowerCLI.

When it comes to making sense of data intelligently, programming can help significantly. PowerCLI combined with some PowerShell is a great weapon to use for this purpose. In this article, I will show a few examples of where PowerCLI is the best tool to use for advanced tasks in vSphere.

vMotion individual hard disks from a datastore ^

In this scenario, I have multiple datastores in my vSphere environment. I am going to be decommissioning a datastore, and I want to move the hard disks to a new datastore. Sure, you can do this through a GUI, but depending on the amount of virtual machines (VMs) on this datastore, this could take a long time. PowerCLI provides a much more efficient way to do this; in fact, you can do it in one command:

In this command, I get a list of the hard disks for VMs running on "olddatastore." But I filter to include only the hard disks that include "olddatastore" in the filename. I then pipe the results to Move-HardDisk, which will vMotion only these hard disks to "newdatastore."

Finding what host a VM is running on ^

If you have used vCenter long enough in a VM, eventually you will run into this issue. You lose vCenter connectivity and suddenly realize you don't know what host it is running on. Sure, you can log in via the GUI to each host and look, or even SSH into each host and find it, but why not let PowerCLI do it for you in a few seconds? I made a small function on Github to do just that, named Get-VCenterEsxiHost.

While the function is intended to find the vCenter VM in a cluster, you can actually use it to find any VM by name. In this example, I want to find the vCenter VM named "vcenter." In the parameters of the function, I need the name of the VM, an array of ESXi hosts, and the credential that can connect directly to the ESXi hosts. As you see below, my vCenter VM is running on VMHost4.

Here, I store the credential for the VMHosts and run Get-VCenterEsxiHost to find the vCenter VM:

Find vCenter VM

Find vCenter VM

Find and remove all snapshots for your entire cluster ^

VM snapshots are great if you are making a change to a server and will soon know whether to roll back that change or not. What you do not want to do is have a VM with a snapshot running for days or weeks. The longer you wait, the larger the snapshot will get, and when you finally go to remove the snapshot, it can be quite frightening due to the time it may take to finish. The Get-Snapshot and Remove-Snapshot cmdlets are perfect for this task.

In this example, I want to find any snapshots in my environment and remove the snapshot. Fortunately, I can also do this with just one command in PowerCLI:

This command gets all VMs from the "Production Cluster" that are currently powered on and have a snapshot. It then pipes the output to Remove-Snapshot and removes them all one by one.

Conclusion ^

For sysadmins who work with large vSphere environments, using command-line tools is necessary. With PowerCLI, admins can reduce the amount of time and effort needed to manage a large vSphere environment while reducing errors associated with manual GUI processes.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!


Users who have LIKED this post:

  • avatar
  • avatar

Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account