Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract the output of Invoke-RestMethod into distinct variables

Somewhat of a Powershell noob here ..

I am working on Microsoft's API for Windows Defender ATP. I need to understand how to extract specific values from a custom powershell object which is returned as the output of Invoke-RestMethod.

$webResponse = Invoke-RestMethod  -Method Get -Uri $url -Headers $headers 
Write-Output $webResponse

This produces the following:

@odata.context value -------------- ----- https://api.securitycenter.windows.com/api/$metadata#Machines {@{id=f7749cafd089c66e53g21332ba0b426f6f88c953; computerDnsName=desktop-h2134uc; firstSeen=4/30/19 10:03:40 PM; lastSeen=5/3/19 4:15:17 AM; osPlatform=Windows10; osVersion…

My question is - how can I extract the individual field values for id, computerDnsName etc.

Thanks!

like image 815
prodman Avatar asked Nov 25 '25 19:11

prodman


1 Answers

$response = Invoke-WebRequest -Uri www.google.at
$response | Get-Member
TypeName: Microsoft.PowerShell.Commands.HtmlWebResponseObject

Name              MemberType Definition
----              ---------- ----------
...
Headers           Property   System.Collections.Generic.Dictionary[string,string]     Headers {get;}
Images            Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;}
InputFields       Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields {get;}
Links             Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Links {get;}
ParsedHtml        Property   mshtml.IHTMLDocument2 ParsedHtml {get;}
RawContent        Property   string RawContent {get;set;}
RawContentLength  Property   long RawContentLength {get;}
RawContentStream  Property   System.IO.MemoryStream RawContentStream {get;}
Scripts           Property       Microsoft.PowerShell.Commands.WebCmdletElementCollection Scripts {get;}
StatusCode        Property   int StatusCode {get;}
StatusDescription Property   string StatusDescription {get;}

Via Get-Member you'll know which properties this object supports. Then you can call these properties on the given object:

$response.StatusCode
200

If you want to select multiple properties you can use Select-Object:

 $response | select statuscode, statusdescription

 StatusCode StatusDescription
 ---------- -----------------
   200 OK

You can, of course, store the results of Select-Object in a variable.

More of less the same can be used fro Invoke-RestMethod:

  Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ | Get-Member

TypeName: System.Xml.XmlElement

Name                 MemberType            Definition
----                 ----------            ----------
...

comments             Property              System.Object[] comments {get;}
creator              Property              System.Xml.XmlElement creator {get;}
description          Property              System.Xml.XmlElement description {get;}
encoded              Property              System.Xml.XmlElement encoded {get;}

...

The properties you can select/use depends on the format returned in the response body. In the above example, you have an XML body, but it could also be JSON or something else.

like image 115
Moerwald Avatar answered Nov 27 '25 18:11

Moerwald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!