{"id":1825,"date":"2023-06-13T09:15:05","date_gmt":"2023-06-13T13:15:05","guid":{"rendered":"https:\/\/enterpriseadmins.org\/blog\/?p=1825"},"modified":"2023-06-13T09:15:05","modified_gmt":"2023-06-13T13:15:05","slug":"using-vi-json-with-powershell","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/using-vi-json-with-powershell\/","title":{"rendered":"Using VI\/JSON with Powershell"},"content":{"rendered":"\n<p>I recently saw a post on a new feature that is enabled by default in vSphere 8.0u1: <a href=\"https:\/\/blogs.vmware.com\/code\/2023\/06\/10\/introducing-vi-json-a-modern-wire-protocol-for-vsphere-management\">https:\/\/blogs.vmware.com\/code\/2023\/06\/10\/introducing-vi-json-a-modern-wire-protocol-for-vsphere-management<\/a>.  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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$vc = 'vc3.example.com'\r\n\r\n# Get SessionManager Value\r\n$sessionManagerMoid = (Invoke-RestMethod \"https:\/\/$VC\/sdk\/vim25\/8.0.1.0\/ServiceInstance\/ServiceInstance\/content\").sessionManager.value\r\n\r\n# Login\/get sessionID\r\n$sessionId = (Invoke-WebRequest -Uri \"https:\/\/$VC\/sdk\/vim25\/8.0.1.0\/SessionManager\/$sessionManagerMoid\/Login\" -Body ( @{'userName'='Administrator@vsphere.local'; 'password'='VMware1!'} | ConvertTo-Json ) -Method:POST -ContentType:'application\/json').Headers.'vmware-api-session-id'\r\n\r\n# Get output from a specific VM\r\n(Invoke-WebRequest -Headers @{'vmware-api-session-id'=$sessionId} -Uri \"https:\/\/$VC\/sdk\/vim25\/8.0.1.0\/VirtualMachine\/vm-48\/config\" -ContentType:'application\/json').Content | ConvertFrom-JSON<\/code><\/pre>\n\n\n\n<p>The above few lines logs in to the API and returns all the details of a specific VM (specifically moref <code>vm-48<\/code> which is passed in via the last URI).  I&#8217;ve included the first few lines of the output below for reference.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_typeName                    : VirtualMachineConfigInfo\r\nchangeVersion                : 2023-06-08T18:12:09.377525Z\r\nmodified                     : 1970-01-01T00:00:00Z\r\nname                         : h006-vm-02\r\nguestFullName                : Microsoft Windows Server 2016 or later (64-bit)\r\nversion                      : vmx-15\r\nuuid                         : 423f0e56-bfe2-39c5-2096-52b59fdea9b8\r\ncreateDate                   : 2022-12-05T13:42:45.640154Z\r\ninstanceUuid                 : 503fd66a-4f92-950d-18fd-1ab0a4f55ff2<\/code><\/pre>\n\n\n\n<p>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. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently saw a post on a new feature that is enabled by default in vSphere 8.0u1: https:\/\/blogs.vmware.com\/code\/2023\/06\/10\/introducing-vi-json-a-modern-wire-protocol-for-vsphere-management. 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 &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/using-vi-json-with-powershell\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-1825","post","type-post","status-publish","format-standard","hentry","category-scripting","category-virtualization"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1825"}],"version-history":[{"count":1,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1825\/revisions"}],"predecessor-version":[{"id":1826,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1825\/revisions\/1826"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}