0

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>
6
  • 1
    "Here's the JSON output" - that's not JSON, that's HTML... Commented Nov 17, 2021 at 12:24
  • @Mathias R. Jessen - right you are. I should have said HTML output featuring JSON data contained within. Let me update that. Commented Nov 17, 2021 at 12:28
  • 1
    Have you seen the comment in the actual 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. So why not do that, receive not an HTML with embedded JSON, but just the JSON response you can use ConvertFrom-Json on. Commented Nov 17, 2021 at 12:57
  • thanks Theo. How would I implement ConvertFrom-Json my code? Can you share an example? Commented Nov 17, 2021 at 13:13
  • 1
    Probably by adding that to the headers Hashtable: $headers = @{ Authorization = "Bearer $cred"; Accept = "application/json" }. Then look at what $Output shows after Invoke-RestMethod. If this is indeed JSON, you can simply do $Output | ConvertFrom-Json and work your way from there Commented Nov 17, 2021 at 16:15

1 Answer 1

1

Maybe this would work. Search for the part of the string containing the JSON data and then convert to an object.

$result = $Output | Select-String -Pattern "var data = {.+}`n"
$lineWithData = $result.Matches.Value
$lineWithData
$json = $lineWithData.Substring('var data = '.Length)
$json
$dataObject = $json | ConvertFrom-Json
$dataObject

Then you can get the ID property from the object.

$dataObject.ID
ad_user://345345-3453-gfdd-44334
local://p-345dd455```
Sign up to request clarification or add additional context in comments.

2 Comments

I get the following error when I run that: ''' You cannot call a method on a null-valued expression. At line:12 char:1 + $json = $lineWithData.Substring('var data = '.Length) '''
Sounds like the $Output variable does not contain a string with the expected pattern, resulting in zero matches and therefore the variable $lineWithData is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.