If you’re a Windows admin who’s recently taken over a VMware vSphere 4 role, you’ll be pleased to know that the...
world of PowerShell you know and love is also available to manage your VMware environment.
Contrary to popular belief, VMware is all about helping admins get their work done, even if that involves piggybacking off Microsoft technology when needed. Enter PowerCLI. Getting PowerCLI setup and ready for use is little more than a “next-next-finish” installation on top of an existing Windows PowerShell installation.
One of the questions frequently asked by Windows admins is if they should learn Linux. My response to this is that you should really forget about learning Linux and the ESX console commands. Not only will those skills be less valuable once ESXi becomes the VMware standard, but there is a relatively small number of commands to be had from the ESX console anyway. In contrast, there are literally hundreds of cmdlets in PowerCLI that offer a level of functionality and control unavailable within the console environment.
Let’s take a look at some of the most important uses for PowerCLI.
Using PowerCLI to … create data centers
Some of the best examples of the value of PowerCLI are the cmdlets that allow you create objects in vCenter. I use these all the time when tearing down, resetting and rebuilding my lab environment, but they can also be used when rolling out a new virtual data center.
Once vCenter is installed, your main task will be to create data centers, clusters and resource pools. If you are looking for consistency and automation you could do all this by using PowerCLI. To create a new data center you would connect to the vCenter in question (Connect-VIServer) and use the new-datacenter cmdlet to create the data center object.
The first part of the PowerCLI script merely connects to vCenter with an administrator account. The second part creates a variable called “$root” to set a location for where the data center is to be created. If the cmdlet get-folder is run with the –NoRecursion switch, it will set the location of the new data center to the top of the vCenter tree. As you can see with my three data centers below (UpTown, MidTown and DownTown), it’s a simple process of using the new-datacenter cmdlets together with the –name variable to set their names.
connect-viservervcnyc.corp –username corp\administrator –password Password1
$root = Get-Folder –NoRecursion
new-datacenter –location $root -name ‘NYC DataCenter - UpTown'
new-datacenter –location $root -name ‘NYC DataCenter - MidTown’
new-datacenter –location $root -name ‘NYC DataCenter - DownTown'
The screenshot below shows the results of each command.
Figure 1. Creating data center objects with PowerCLI
Using PowerCLI to … create clusters
Of course, the next thing you’d want to do is start creating VMware clusters inside each of your data centers. This can be achieved by using the new-cluster cmdlet, which supports many options to help configure your cluster properties of VMware High Availability (HA) and Distributed Resource Center (DRS) with ease. In the PowerCLI script below, I’ve created a cluster called “Cluster” within the “NYC DataCenter – DownTown”. The clever stuff really comes in the additional parameters:
new-cluster –location (get-datacenter –name 'NYC DataCenter - DownTown') -name 'Cluster' -HAEnabled -HAAdmissionControlEnabled –HAFailoverLevel 2-DRSEnabled –DRSMode PartiallyAutomated
So using -HAEnabled enables VMware’s HA and DRS technology on the cluster. Having the -HAFailover level set to 2 indicates that I can tolerate the loss of a maximum of two ESX hosts before failover of virtual machines would stop. It’s a simple way of making sure that the failure of a significant number of hosts doesn’t result in the system becoming overloaded.
The DRS mode parameter has three options (Partially Automated, Fully Automated and Manual), with each offering more and more automation. For example, when a virtual machine is powered on, the Partially Automated option allows the cluster to decide which ESX host the VM should run on. Keep in mind the administrator would still receive notifications of when DRS thought it was a good idea to move a VM and essentially control the radio buttons in the main DRS part of the cluster dialog box.
Using PowerCLI to … create resource pools
Perhaps the next stage would be to define resource pools on the VMware cluster. As you may know, VMware resource pools are a way of taking the large resources of VMware clusters and cutting them down to manageable units. It’s not unusual to create a resource pool for each application type (one for Active Directory domain controllers, another for SQL Servers, and so on). This can be automated using the new-resourcepool cmdlet. As with the other cmdlets we’ve used, this one requires a location parameter set together with the name of the resource pool to be created. This can then be repeated for each resource pool you need to create.
new-resourcepool –location (get-datacenter –name 'NYC DataCenter - DownTown' | get-cluster –name 'Cluster') -name ‘Web Servers’
new-resourcepool –location (get-datacenter –name 'NYC DataCenter - DownTown' | get-cluster –name 'Cluster') -name ‘Database Servers’
The interesting thing about using PowerCLI in this way is that you normally would need ESX hosts added to the cluster before you could create resource pools. With PowerCLI, however, you can create your resource pools before you add your ESX hosts. It’s a good example of how PowerCLI can carry out tasks that are usually denied via the vSphere client.
Figure 3. Creating resource pools with PowerCLI
Using PowerCLI to … add ESX hosts
The next big task an admin would need to carry out involves adding ESX hosts to the cluster. This can be done using the add-vmhost cmdlet within PowerCLI.
add-vmhost esx1.corp.com -location (get-datacenter –name 'NYC DataCenter - DownTown' | get-cluster –name 'Cluster') -user root –password Password1 -force
Using the get-datacenter and get-cluster cmdlets together, we can specifically target an object in vCenter as the correct location for an ESX host to be added to the inventory. Adding them to a HA cluster will also trigger the start of the HA agent to the ESX host.
Here the -force parameter is used to accept the default SSL certificate generated during the install of an ESX host. Without the use of the -force parameter, you would need fully-trusted SSL certificates to successfully add an ESX host to vCenter. Otherwise the task would fail with an error stating “Unable to verify the authenticity of the specified host because it is using a self-signed SSL certificate.”
Figure 4. Adding ESX hosts to a cluster
As you can see, PowerCLI is extremely… powerful. Its value comes from the ability to automate tasks simply and easily. Once you’ve mastered the basics of Windows PowerShell, learning the additional cmdlets from VMware is relatively easy.
Year after year, VMware adds more cmdlets to make your daily administration life easier, but you can go even further beyond those cmdlets if you’d like. PowerCLI can interface with VMware’s SDK via a web services engine and practically any tasks you carry out in the vSphere client can be automated with PowerCLI through the SDK. It’s a good option to have until VMware brings out a new version of PowerCLI that allows you to access the configuration options you need with a simple cmdlet.
You can follow SearchWindowsServer.com on Twitter @WindowsTT.
ABOUT THE EXPERT
Mike Laverick (VCP) is an award-winning expert and author who has been involved with the VMware community since 2003. He is a VMware forum moderator and member of the London VMware User Group Steering Committee. Laverick is the owner and author of the virtualization website and blog RTFM Education, where he publishes free guides and utilities aimed at VMware ESX/VirtualCenter users.