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.

Read more on 4sysops.com

Categories: Technical

Leave a Reply

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