everythingpossible - Fotolia

Tip

VMware Workstation 15's built-in REST API eases VM automation

You can use Workstation 15's new REST API to automate VM tasks. Simply enable the GUI and sort tasks with a PowerShell script using either the get or put method.

VMware Workstation's REST API integration could potentially make a big difference in how you work. For example, instead of manually powering on machines, you can now program multiple machines to power on automatically while you make coffee.

VMware Workstation has integrated with programming languages such as Perl and C in the past, but the integration was clunky. Because these languages were difficult to use, most IT administrators never fully utilized them. The REST API in Workstation 15 provides a language-agnostic way to take advantage of the power of the Workstation API. Along with powering VMs on and off automatically, this feature enables you to automate VM configuration, cloning and removal. You can also suspend, pause, and resume VM operations as needed.

Incorporating automation into your management strategies is relatively easy. To get started, simply follow these steps to enable the GUI, interrogate information and modify scripts.

Enable the GUI

The GUI sorts and groups tasks by request type, which provides the flexibility to implement just about anything you could need in terms of local VM management.

To enable the GUI, you don't have to configure the Secure Sockets Layer portion to experiment locally, but you should if you want remote access.

API explorer setup
Figure A. Set up the API explorer.

To access the web console and see these commands from your Windows desktop, navigate to the correct URL -- usually localhost:8697 -- and use the credentials you just created. Once authorized, you can expand the webpage and experiment with various operations to familiarize yourself with the capabilities of the API in Workstation 15.

use API explorer
Figure B. Experiment with the API explorer.

Use PowerShell with Workstation 15

The next step is to chain these commands together into some form of automation. One of the best methods is with the built-in PowerShell scripting language. You can use the API to automate VM tasks in several ways.

First, sort authentication with the following script:

$Username = "stuart"
$apiURL = "http://localhost:8697/api"
$Password = "MyPassw0rd!"

$authInfo = ("{0}:{1}" -f $Username,$Password)
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
$authInfo = [System.Convert]::ToBase64String($authInfo)
$headers = @{Authorization=("Basic {0}" -f $authInfo)}

$contentStyle = "application/vnd.vmware.vmw.rest-v1+json"

$response = Invoke-RestMethod -Uri $apiURL/vms -Method Get -Headers $headers
$uuid = $response.id

Set and include the $apiURL variable, rather than typing out the full path every time, because it's more efficient.

By default, this code returns two properties with the variable "$response": the VM id and the configuration file. It also lists every VM in use.

VM information
Figure C. Automatically get VM information.

The "id" field is a unique VM identifier; there's one for each VM, and every operation requires a unique ID. $uuid grabs the unique identifier from the response data.

VM power status
Figure D. Get the power status of a VM.

Get vs. put method

The "get" method interrogates a VM, but you can also use the "put" method. Think of "get" as the way to simply interrogate information and "put" as the way to make changes.

At this point, you can modify the script to power on the VM. You can use Invoke-RestMethod, but with "put" instead of "get." To power on the VM, pass "on" in the body. To power it off, pass "off:"

Invoke-RestMethod -Uri "$apiURL/vms/$uuid/power"  -Method PUT -Headers $headers  -Body on -ContentType $contentStyle

Figure E below shows the parameters for the method and the id -- the $uuid variable, in this case -- as a string. It also shows the operation type, located in the body, with an example of allowed values.

Workstation 15's REST API comes with a GUI, which enables you to experiment with API calls.

GUI API browser
Figure E. Get the available options using the GUI API browser.

Once you know how to use the API, you can combine that with a text file, a "for" loop and the get-content command to automatically set up or power down various VMs without human intervention. You can also clone VMs or configure hardware as needed.

Dig Deeper on VMware desktop software and virtualization

Virtual Desktop
Data Center
Cloud Computing
Close