Editor’s Note: As of January 2022, iland is now 11:11 Systems, a managed infrastructure solutions provider at the forefront of cloud, connectivity, and security. As a legacy iland.com blog post, this article likely contains information that is no longer relevant. For the most up-to-date product information and resources, or if you have further questions, please refer to the 11:11 Systems Success Center or contact us directly.
We often get questions from customers about the best way to automate their cloud environments. As 11:11 Systems provides Infrastructure-as-a-Service based on VMware vCloud Director, we expose a RESTful API that can be used to automate things at the vCD level.
We also offer a higher-level REST API for the 11:11 Cloud Console, which exposes other services that 11:11 has curated into the console, such as vSphere performance data, security and compliance data, and disaster recovery services with Zerto.
More information on the 11:11 REST API is available here.
As well as RESTful APIs, another set of tools that leverage these APIs has proved extremely popular over the past few years: Microsoft Powershell and the extensions provided by VMware as PowerCLI provide an excellent environment to automate a VMware-based cloud infrastructure, such as that provided by the 11:11 Cloud Platform.
Anyone who has done any development during their career should be able to understand the syntax of PowerCLI for simple scripting or full-blown applications.
Microsoft provides an excellent integrated scripting environment (ISE) that enables a PowerCLI developer to easily discover commands, known as cmdlets, and interrogate the data that is returned from these. The IntelliSense capability helps to automatically complete commands and object names.
We don’t propose to go into great detail about how to get PowerCLI running, but suffice to say it can be downloaded from Microsoft and VMware websites. In recent news, Powershell, which traditionally only ran on Windows operating systems due to its dependence on .NET, has just been ported to both Linux and Apple Mac. PowerCLI cmdlets for Linux and Mac should be available soon. In the meantime, you may use PowerCLI running on Windows 10 on Mac inside VMware Fusion.
Powershell ISE can be made to automatically load all the PowerCLI cmdlets using the profile feature, whereby you can specify all the PowerCLI. Several additional PowerShell functions have been written by ecosystem members that can be included in this way and can be downloaded from various forums and blogs on the Internet.
PowerCLI can be used for a variety of automation tasks. Generally speaking, there are a small number of main functions:
- GET – retrieve information about stuff
- SET – change attributes
- NEW – create new things (but also ADD)
- REMOVE – delete things
- START/STOP/SUSPEND — operates virtual machines
- CONNECT/DISCONNECT — log in and out of a vCloud environment
The full set of PowerCLI cmdlets can be found at: http://pubs.vmware.com/vsphere-60/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2FOverview.html
Simple GET cmdlets
A good starting point is querying the vCloud environment using GET commands. As the name suggests, this gets information without changing anything, so it is safe for beginners!
Before we can do anything, we’ll need to connect to the vCloud environment, using the same URL as indicated by the 11:11 Cloud Console interface when you choose to interact with vCloud Director.
The PowerCLI cmdlet Connect-CIserver is used with –server, -user, -password, and –org to specify those details.
Having successfully connected, you can start running GET commands. Many of the vCloud cmdlets have ‘CI’ in front (cloud infrastructure), to differentiate them from vSphere/vCenter cmdlets. So, Get-CIVM instead of Get-VM.
In the example below, Get-OrgVdc is used to get information about our two Org Virtual Data Centres, both of which are pay-as-you-go.
Then, Get-CIVapp is used to list all the vApps that are currently deployed. Finally, list all the VMs using Get-CIVM.
You can use the format table command (ft) to format the output neatly and specify the column headings you’d like to see.
Rather than using individual cmdlets on the command line, you can start to produce scripts that perform a similar function to the above.
In the example below, $vApps is an array that contains all the vApps and their configuration data, while $VMs is an array for all the VMs that are discovered. $vApp and $VM are then objects which contain individual data.
The IntelliSense feature within Powershell ISE allows you to see what information is stored within an object.
When the script is executed, the following output is produced. Rather than writing to the screen, it could be exported to an Excel or CSV file.
Automating the deployment of vApps
PowerCLI can be used to deploy new vApps from existing vApp templates within the vCloud catalog. This could be either from your own catalog or from the public catalog offered by 11:11. The cmdlet get-civapptemplate will show you all the vApp templates available.
In this example, we want to deploy the Travel App vApp template from the test catalog and call the new vApp “Travel App 1.”
This short script deploys the new vApp in a matter of minutes. By default, the vApp will be powered off.
We may want to make some changes to the VMs within the vApp once deployed. For example, you may want to give the “MySQL server” 4GB of RAM – it currently has only 750MB.
Unfortunately, there isn’t a nice, easy Set-CIVM cmdlet, so we need to do some jiggery-pokery using extension data. Fortunately, a quick trundle ’round the Internet found the commands to use.
And, we can see that the RAM value has been increased on the “MySQL server” VM.
Finally, the vApp can be started up.
Hopefully, this short article has been useful to whet the appetite for Powershell and PowerCLI.
Watch this space for more articles on this exciting topic!
For further reading, we can recommend our VMware friend and colleague, Alan Renouf, and his blog site at http://www.virtu-al.net/ – what he doesn’t know about PowerCLI isn’t worth knowing!