Powershell: How to get REST API data in JSON format rather than XML using invoke-restmethod

Summary:
I was exploring a REST API interface for an internal tool being built.  Being that I'm so accustomed to powershell, I wanted to explore how I could get data from it.  The Invoke-RestMethod is perfect for this, but I was having issues getting data back in straight json format.  Data kept coming back in ugly as hell xml format by default.

Details:
The short answer was that I need to make a hash table to pass to the -Header parameter of the invoke-method cmdlet.  Basically, it looks like this:

$Headers = @{"Accept" = "application/json"}
Invoke-RestMethod -URI "https://myrestapi/endpoint" -Method:Get -Headers $Headers 

Once I did this, I received the data back in json format and powershell automatically captures it as a system.array object.  Making it immensely easier to work with rather than the xml return.  See below pictures as examples of the difference.
Json returned data.

xml returned data
As you can see, the return I received when in json looks like any other object return from something like powercli whereas the xml return is this ridiculous mess.  Not all Rest API endpoints work in the same fashion.  Some will return json by default, but listing the "Accept" = "application/json" in your request header doesn't seem to hurt those that don't unless you want a different type of return.


VMware: ESXi 6 503 Service Unavailable endpoint: [N7Vmacore4Http16LocalServiceSpecE:0x1f098b08] _serverNamespace = / _isRedirect = false _port = 8309)

Summary:
Basically I enabled SR-IOV on the only two pNIC's I had in my ESXi host in my lab.  This doesn't necessarily cause a connectivity problem, but the ESXi management agents did not like this at all.  Meaning I could connect to my hosts, as evident in the error message, but the agents basically broke once SR-IOV was enabled on the only two physical uplinks I had.



Workaround:
Unfortunately, the only workaround I've found is to:

  • "Reset System Configuration" from DCUI
    • This basically bring ESXi back to default install config.  root password is blanked out, etc. etc.
or
  • Re-Deploy the host. 
For my testing though, I can enable on one of the physical uplinks and work w/ that just fine, just not both in my case.  The other aspect that I didn't realize is that the CNA cards I was using lose their Fiber Channel connectvity as well.

SR-IOV effectively changed my CNA cards to NIC adapters only.

Config:
ESXi 6.0 Build 5050593
Dell FX2 - FC630 - Qlogic 57810 adapters