April 10, 2017
Introduction to VMware vSphere Automation with PowerCLI
PowerCLI is one of the most powerful means to automate the installation, VM provisioning, changes, and really any aspect of VMware vSphere. Anytime we can automate tasks that are performed frequently or at a certain interval, we are using our time as administrators much more efficiently. Let’s take a look at what PowerCLI is as well as some of the basics of learning to use it to automate a VMware vSphere installation.
What is PowerCLI?
PowerCLI is VMware’s PowerShell modules that extends PowerShell to understand and interact with VMware environments. During installation, it installs the VMware specific PowerShell commandlets that allow you to work with VMware vSphere environments. Commandlets are commands used in a Windows PowerShell environment.
The latest version of PowerCLI as of this writing is PowerCLI 6.5 Release 1 which can be downloaded here. If you already have it installed and want to see which version you are running, launch PowerCLI and run the following commandlet:
After downloading the installer, simply begin the installation by executing the installer.
After clicking next through the wizard, the installer should finish successfully.
The PowerCLI shortcuts are installed to the desktop by default after the installation finishes successfully.
The shortcuts contain the following in their paths:
- 64 bit – C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c “. \”C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\” $true”
- 32 bit – C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c “. \”C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\” $true”
As you can see from the shortcut paths, they are simply loading up PowerShell and running the respective scripts to initialize the environment.
Integration with Windows ISE
Many who develop PowerShell scripts prefer to use the built-in Windows Integrated Scripting Environment to write, test and debug scripts. Can you integrate VMware PowerCLI into the Windows PowerShell ISE? Yes! We need to add the appropriate line in our Windows PowerShell ISE profile when it loads. To load PowerCLI in Windows PowerShell ISE, simply add the following to our Windows PowerShell ISE profile:
# Load PowerCLI commandlets
& ‘C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1’
There are many benefits to using Windows Powershell ISE running our PowerCLI scripts. When using the Windows PowerShell ISE, we have access to multiline editing, tab completion, syntax coloring, selective execution, context-sensitive help, and others. Notice the “tab completion” below.
In this post we have seen what PowerCLI is, how to get it installed on a client as well as how to integrate it into the Windows PowerShell ISE that is baked into current Windows versions. Next, we will take a look at basic PowerCLI scripting including connecting to ESXi or vCenter, its commandlets, and looping reading from files.