Sunday, February 21, 2016

Interacting With The App Volumes REST API Through PowerShell

In December of last year Chris Halstead created an App Volumes API reference that blew my mind. (Check it out here:VMware App Volumes API Reference) He walks you through executing App Volumes management functions remotely using it's REST API and Google Postman as a client. After replicating the process in my lab it felt natural to ask, "can I do this in PowerShell natively?" Well, I went kind of crazy figuring out how but finally had some success. It's all about leveraging the Invoke-RestMethod cmdlet. You can find info about it here: Invoke-RestMethod

The first order of business is to login into the API. This is how:

$body = @{
username = “justin"
password = “VMware1!”

Invoke-RestMethod -SessionVariable DaLogin -Method Post -Uri "http://appvolumes.lab.local/cv_api/sessions” -Body $body

So above, "justin" is my AD username and "VMware1!" is the password for that account. "Appvolumes.lab.local" is the name of my App Volumes server. Here's the output:

Using the -Session variable option when executing this cmdlet is key. It creates a web request session object you use to authenticate in subsequent commands. In the command above the web request session object is assigned to a variable called ‘DaLogin’. I can now leverage this variable going forward when making API calls. For example, I can obtain info about App Volumes users with the following command:

Invoke-RestMethod -WebSession $DaLogin -Method Get -Uri "http://appvolumes.lab.local/cv_api/users”

Or, I can obtain information about app stacks with this:

Invoke-RestMethod -WebSession $DaLogin -Method Get -Uri "http://appvolumes.lab.local/cv_api/appstacks”

And, since we're talking powershell, we can easily pin point specific pieces of info we're looking for.

Invoke-RestMethod -WebSession $DaLogin -Method Get -Uri "http://appvolumes.lab.local/cv_api/appstacks” | FT id, name, created_at

Now, using the id # gleamed for the last command I can go on to assign and attach an app stack to a user. Here's the command:

Invoke-RestMethod -WebSession $DaLogin -Method Post -Uri "http://appvolumes.lab.local/cv_api/assignments?action_type=Assign&id=1&assignments%5B0%5D%5Bentity_type%5D=User&assignments%5B0%5D%5Bpath%5D=CN%3Djustin%2CCN%3DUsers%2CDC%3Dlab%2CDC%3Dlocal&rtime=true&mount_prefix="

These are just a few examples of some of the shenanigans we can get into using PowerShell and the App Volumes REST API. For additional examples and details of different API calls you should definitely check out Mr. Halstead's blog.