Ever needed to do some bulk operation in you’re vmware virtual environment ?
Or just needed information of a bunch of virtual machines ?

Then you probably had a lot of manual labor to do in the vShere client.
Well vmware came with  a solution:

The program is called VMware vSphere PowerCLI and more information can be found here.

A short discription in the words of vmWare:

VMware vSphere PowerCLI provides a Windows PowerShell interface to the vSphere API. vSphere PowerCLI includes PowerShell Cmdlets (pronounced, “command-lets”) for administering vSphere components. In addition, the vSphere PowerCLI package includes the vSphere SDK for .NET for developers who want to create their own applications.

Let’s get started implementing and using the PowerCli

Before we begin we have some prerequisits:

.NET 2.0 SP1 and Windows PowerShell 1.0/2.0 must be installed

We also need to enable the ‘Execution Policy’ of PowerShell

To change the ‘Execution Policy’ start a PowerShell session with Administrator Privileges

In the powershell command box type:

Set-ExecutionPolicy RemoteSigned

You will then receive information about execution policies and a prompt asking you to confirm your action before changing the execution policy, enter Y at the prompt and press enter.

Now we are ready to install the PowerCli Program (it can be downloaded here)

The installation of this program is pretty  straight forward:

  • Start the installer exe (on the time I’m writing this, the name of the exe is VMware-PowerCLI-4.1.1-332441.exe)
  • Read the license agreement and accept it
  • Click Next until you get the finish button
  • Click Finish 🙂

To check that the installation is working open the shortcut that is created on you’re desktop called VMware vSphere PowerCLI or if you want the long way: Start->Programs->VMware->VMware vSphere PowerCLI->VMware vSphere PowerCLI

A DOS like command box will open:

Now we will connect to the vCenter Server by typing in the following command

Connect-VIServer -Server <vCenter Server Ip or FQDN>

If you are logged in with a user that has access on vCenter, no password will be asked, if you are not logged in, a login prompt will appear.

Here you can log yourself in with the same credentials as you are going to login to the vsphere client.

A way to bypass the login prompt is providing you’re credentials in the connect command

Connect-VIServer -Server <vCenter Server Ip or FQDN>
       -protocol https -User <vCenter Username>
       -Password <vCenter Password>

Some little commands you can run to try the PowerCli out:

  • get-vm :  retrieve a list of the virtual machines
  • get-virtualswitch : retrieve a list of the vswitches

A lot more commands are available, basically everything you can do in the gui (vshere client) you can do in PowerCli and much more.

A nice reference sheet can be found here.

A small example to show you how powerfull powercli can be

Get-VMHost | Select @{N=“Cluster“;E={Get-Cluster -VMHost $_}},
Name, @{N=“NumVM“;E={($_ |
Get-VM).Count}} | Sort Cluster, Name

With this one line script, we made a list of all the host with a calculation of the number of vm’s that are on that host.

If you can see what you can do with one line imagine what you can do with a whole script 😛

I hope I got you started using PowerCli …