I have created a PowerShell script to return values from a web URL featuring JSON data.
My script is now returning HTML with some JSON data included.
Now, I want to retrieve a value from the JSON data contained within this HTML.
Specifically, the value for 'ID.ad_user' where the data variable is declared ("var data = ").
So, "345345-3453-gfdd-44334" is the value I need (exlcuing the "ad_user://" text).
Here's my PowerShell code:
$cred = "[my unique token]"
$headers = @{ Authorization = "Bearer $cred" }
$Output = Invoke-RestMethod `
-Method Get `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-Uri "[my URL]"
$Output
Here's the HTML & JSON output:
<!DOCTYPE html>
<!-- If you are reading this, there is a good chance you would prefer sending an
"Accept: application/json" header and receiving actual JSON responses. -->
<link rel="stylesheet" type="text/css" href="/ui.min.css" />
<script src="/ui.min.js"></script>
<script>
var user = "admin";
var curlUser='[HIDDEN]';
var data = {"name": "Pat McKinnon","ID": ["ad_user://345345-3453-gfdd-44334","local://p-345dd455"],"state": "active",}
</script>
If you are reading this, there is a good chance you would prefer sending an "Accept: application/json" header and receiving actual JSON responses.So why not do that, receive not an HTML with embedded JSON, but just the JSON response you can useConvertFrom-Jsonon.$headers = @{ Authorization = "Bearer $cred"; Accept = "application/json" }. Then look at what$Outputshows afterInvoke-RestMethod. If this is indeed JSON, you can simply do$Output | ConvertFrom-Jsonand work your way from there