Using VI/JSON with Powershell

I recently saw a post on a new feature that is enabled by default in vSphere 8.0u1: The article describes a new JSON-based, REST-like protocol named VI/JSON that allows access to all the familiar SOAP XML properties. It seemed interesting, so I decided to try it out with PowerShell.

$vc = ''

# Get SessionManager Value
$sessionManagerMoid = (Invoke-RestMethod "https://$VC/sdk/vim25/").sessionManager.value

# Login/get sessionID
$sessionId = (Invoke-WebRequest -Uri "https://$VC/sdk/vim25/$sessionManagerMoid/Login" -Body ( @{'userName'='Administrator@vsphere.local'; 'password'='VMware1!'} | ConvertTo-Json ) -Method:POST -ContentType:'application/json').Headers.'vmware-api-session-id'

# Get output from a specific VM
(Invoke-WebRequest -Headers @{'vmware-api-session-id'=$sessionId} -Uri "https://$VC/sdk/vim25/" -ContentType:'application/json').Content | ConvertFrom-JSON

The above few lines logs in to the API and returns all the details of a specific VM (specifically moref vm-48 which is passed in via the last URI). I’ve included the first few lines of the output below for reference.

_typeName                    : VirtualMachineConfigInfo
changeVersion                : 2023-06-08T18:12:09.377525Z
modified                     : 1970-01-01T00:00:00Z
name                         : h006-vm-02
guestFullName                : Microsoft Windows Server 2016 or later (64-bit)
version                      : vmx-15
uuid                         : 423f0e56-bfe2-39c5-2096-52b59fdea9b8
createDate                   : 2022-12-05T13:42:45.640154Z
instanceUuid                 : 503fd66a-4f92-950d-18fd-1ab0a4f55ff2

This is an interesting way to get JSON output of objects and can allow for integrations with newer tooling. I hope this PowerShell example is helpful.

This entry was posted in Scripting, Virtualization. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *