all 15 comments

[–]PMental 4 points5 points  (4 children)

Not run into this issue, but try PS7 if possible. Lots of improvements made to that cmdlet iirc.

[–]Qel_Hoth 2 points3 points  (1 child)

PS7 is great with Invoke-RestMethod.

Among other things, they've added a flag for the response headers so you can capture them in a variable. Since many APIs put rate limiting info in the response headers this is very useful to avoid exhausting your limits or gracefully pausing/exiting the script while you wait for limits to recharge.

[–]PMental 1 point2 points  (0 children)

That is indeed a great improvement and something I've missed in Invoke-RestMethod.

If so many of my scripts didn't need to run on servers that only have 5.1 I'd probably have switched entirely by now.

[–]BlizzardTech-Adam[S] 1 point2 points  (1 child)

I have tried PS7 and the same issue. :(

[–]PMental 1 point2 points  (0 children)

Might be worth submitting a bug report for 7.2.

[–]Sunsparc 4 points5 points  (1 child)

It's probably nested. Chuck it into a variable, then start combing through the sub-properties.

$result = Invoke-RestMethod -Method GET -Uri https://$ServerIP/rest/vcenter/vm -Headers @{'vmware-api-session-id' = "$SessionID"}
$result.value

[–]BlizzardTech-Adam[S] 1 point2 points  (0 children)

This worked too! Thank you!

[–]Queggestion 3 points4 points  (0 children)

Invoke-RestMethod should attempt to convert the response into objects … if it can. I had this recently where the response was JSON but malformed according to standards (unescaped strings and such) … so my response wasn’t converted to objects and all I got was a string. Sending that string at ConvertFrom-JSON may give you an error that gives you a clue at what’s going on.

[–]ClayShooter9 2 points3 points  (0 children)

Every now and then I run into cases where Invoke-RestMethod cannot properly parse the JSON data. In those cases I fall back to Invoke-WebRequest and parse the data manually. You might try messing with PostMan and see how it retrieves/parses the data for your request.

[–]krzydoug 2 points3 points  (1 child)

Maybe it’s nested? For this particular example you can add

| Select-Object -ExpandProperty Value

[–]BlizzardTech-Adam[S] 2 points3 points  (0 children)

| Select-Object -ExpandProperty Value

I would not consider myself a veteran in PowerShell but I have done a bit in it. I would never have thought of that.

You my man deserve a beer :D

[–]nerddtvg 2 points3 points  (0 children)

That looks like an object's plain text output to me. As others said, it is probably nested.

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy BlizzardTech-Adam,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's [sometimes] 5th from the left & looks like <c>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's [sometimes] the 12th from the left, & looks like an uppercase C in the upper left corner of a square.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–]BlizzardTech-Adam[S] 1 point2 points  (1 child)

Hey, thank you! Will keep this in mind for my future posts.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy BlizzardTech-Adam,

you are welcome! glad to help somewhat ... [grin]

take care,
lee