During the VMware Fusion 2017 Tech Preview, I was experimenting around with the new Fusion REST API and I had built a small prototype PowerShell Module as a way for me to learn how the API works. This allowed me to provide valuable feedback back to the Fusion Engineering team on improving the REST API UX. I was pleasantly happy to see that the majority of the feedback was indeed implemented for Fusion 10 which GA'ed a few weeks back.
Given the PowerShell module was pretty useful for my own use, I figure I would also publish it for others who might also be interested in Automating VM management using the new Fusion REST API, especially those with a PowerShell/PowerCLI background. Another nice thing about the module is that it can run across macOS/Linux via PowerShell Core or Windows using full blown PowerShell. I have been slowly tweaking the module to include the updated REST API changes and I am please to announce that the VMware.Hosted PowerShell Module which supports the new Fusion 10 REST API is now available!
The module includes the following 14 functions:
- Connect-HostedServer
- Disconnect-HostedServer
- Get-HostedNetworks
- Get-HostedVM
- Get-HostedVMNic
- Get-HostedVMSharedFolder
- New-HostedVM
- New-HostedVMSharedFolder
- Remove-HostedVM
- Remove-HostedVMSharedFolder
- Resume-HostedVM
- Start-HostedVM
- Stop-HostedVM
- Suspend-HostedVM
If you have ever used PowerCLI before, these functions should feel very familiar. We have basic Connect/Disconnect-HostedServer which will set an environmental variable called $DefaultHostedServer. This variable contains some basic information about the Fusion API endpoint as well as the base64 encoded credentials which are required when connecting to the new Fusion API. Below are a few examples using the new Fusion module, they are pretty basic and I have only implemented a sub-set of the Fusion REST API, so any community contributions are most welcome!
Lets connect to the Fusion 10 REST API endpoint remotely using the Connect-HostedServer function, which means HTTPS will need to be setup (see this blog post here for details):
Connect-HostedServer -Server 172.16.1.1 -Protocol https -Username vmware -Password 'VMw@re123'
Next, we can get a list of all VMs that are registered with our Fusion instance by using the Get-HostedVM function:
Get-HostedVM
To get more details about a specific VM, we can also pass in the VM's Id as shown in the screenshot:
Get-HostedVM -Id 1TS7DAP
Today, the REST API only allows for Cloning from an existing (poweredOff VM), so lets try that out using the New-HostedVM function which accepts the Id of an existing VM and the new name for the cloned VM:
New-HostedVM -ParentId 1tS7DAP -Name Photon-Clone
To power on the VM, we simply use the Start-HostedVM and passing in the Id of the VM:
Start-HostedVM -Id 1CW6DN
Now that the VM is powered on, we can retrieve more details such as the power state and the IP Address of the VM by running the Get-HostedVM function:
Get-HostedVM -Id 1CW6DN
I personally make use of the Shared Folders feature in Fusion, so I created a few functions to allow you to retrieve, configure and remove shared folders for a given VM.
To create a new shared folder mapping, we can use the New-HostedVMSharedFolder function and providing the Id of the VM, the name of the folder inside of the VM and the full host path that we wish to share within the VM:
New-HostedVMSharedFolder -Id 1CW6DN -FolderName Downloads -HostPath /Users/lamw/Downloads
As you can see in the example below, I created two shared folders and we can list all shared folders for a given VM by simply running:
New-HostedVMSharedFolder -Id 1CW6DN
Finally, we can also list all VM Networks that have been defined in Fusion and their configurations by using the Get-HostedNetworks command as shown in the screenshot below:
There are several other functions that I did not cover, but hopefully you will find that they are pretty easy and intuitive to use. Since this a community and non-official VMware module, if folks would like to contribute and add other functionality or even improve the current implementation, feel free to send me a pull request.
I hope the REST API will be available in Workstation at some point. I found a thread on VMTN that they hope it will come, so fingers crossed!