Lab Automation with PowerCLI Part 1: Get setup, write a quick script
In recent months I have come to appreciate the functionality VMWare exposes through PowerCLI. This is the first article in a series documenting the use of PowerCLI in a vCenter/ESX environment.
Next article: Gathering performance statistics
IMPORTANT NOTE: VMWare considers open API access in the Free-licensed version of ESXi 5 to be a bug. It is possible that free access to the APIs will be removed in an update. You might want to keep a copy of your 5.0 ISO handy. :)
Notes:
- Powershell 2 (Download)
- ESXi 5 (Download, Free the last time I checked)
- PowerCLI v5 (Download)
- Powershell ISE (Integrated Scripting Environment, should come with Powershell)
- Developer guide PDF
- Users guide PDF
To get started, you will need to download and install a few components:
- Powershell 2.0 (If you already run Windows 7 or 2008 R2, skip this)
- PowerCLI 5
.. Be sure to note the message that pops up during installation. You may want to run the command to disable script signing on your box to make running scripts easier. This is NOT recommended for a production environment:
Set-ExecutionPolicy unrestricted
Note: It kind of goes without saying that you need to have an ESX(i) box up and running to make any use of this article series
Once you Have Powershell and PowerCLI Installed, Click on Start and type Powershell ISE Click on Windows Powershell ISE to get started.
Run This command to activate the PowerCLI addtions in ISE:
Add-PSSnapin "Vmware.VimAutomation.Core"
Here's a screenshot indicating what should happen at this point:
If successful, you will see a message indicating that the addin has been loaded.
NOTE: This will need to be run first every time you start Powershell ISE
At this point you are free to start developing powerCLI scripts.
Here is a sample to get you started. This script will connect to an ESX(i) host and return a list of VM names:
#Add-PSSnapin "Vmware.VimAutomation.Core"
Connect-VIServer -Server 10.10.10.100 -User root -Password SOmePassword
$VMs = Get-VM
foreach($VM in $VMs)
{
Write-Host $VM.name
}
Disconnect-VIServer -Server 10.10.10.100
NOTE: The first line is commented out since we already loaded the PowerCLI module.
Here is a screenshot of the results from my ESXi box:
We'll cover more complex scripts in the following tutorials. For now I recommend reading through the documentation listed above in the Notes section.
Next article: Gathering performance statistics