Convert cURL to PowerShell Invoke-WebRequest - JSON data table issue - json

I have the following PowerShell script that uses cURL in Windows 10 and works perfectly:
$Body = #{
'data' = #{
'CID' = 15;
'HID' = 37;
'Type' = "TYPE1";
'downloadOn' = "NEXT_CONTACT";
'AutomationEnabled' = "True";
}
}
$CurlArgument = '-s', '-X', 'PATCH',
'-H', 'Content-Type: application/json',
$URL,
'-H',
$AuthBearer,
'-d',
(($Body | ConvertTo-Json) -replace '"', '\"')
Write-Host "$Section cURL command took" ("{0:n1}" -f (Measure-Command {$UpdateResponse = & $CURLEXE #CurlArgument}).TotalSeconds) "Seconds" -ErrorAction SilentlyContinue
I can't use cURL, I need to use native Invoke-WebRequest on my production servers. I need to convert the above into a Invoke-WebRequest command, which I have done, as follows:
$Body = #{
'data' = #{
'CID' = 15;
'HID' = 37;
'Type' = "TYPE1";
'downloadOn' = "NEXT_CONTACT";
'AutomationEnabled' = "True";
}
}
(($Body | ConvertTo-Json) -replace '"', '\"')
$Method = "PATCH"
$Header = #{"Accept" = "*/*" ; "Cache-Control" = "no-cache" ; "Host" = "myURL"; "accept-encoding" = "gzip,deflate"; "Authorization" = "Bearer $SessionToken" }
$ContentType = "application/json"
Write-Host "$Section Invoke-WebRequest command took" ("{0:n1}" -f (Measure-Command { $UpdateResponse = Invoke-WebRequest -Method $Method -Uri $URL -Header $Header -ContentType $ContentType -Body $Body }).TotalSeconds) "Seconds"
When I run the Invoke-WebRequest, I get the following error, i.e. A JSONObject text must begin with '{':
Invoke-WebRequest : {"status":"FAILURE","errors":...."message":{"5011":"A JSONObject text must begin with '{' at 1 [character 2 line 1]"}}]}
My $Body looks like this i.e. it begins with '{' :
{
\"data\": {
\"downloadOn\": \"NEXT_CONTACT\",
\"HID\": 37,
\"AutomationEnabled\": \"True\",
\"CID\": 15,
\"Type\": \"TYPE1\"
}
}
I tried with and without "-replace '"', '\"'", from this post"
cURL to PowerShell - Double hash table in --data?
Looking at my $Body JSON "object"(?), I can see this:
Name Value
---- -----
data {downloadOn, HID, AutomationEnabled, CID...}
Looking at my Value, I can see it is listed as follows:
Name Value
---- -----
downloadOn NEXT_CONTACT
HID 37
AutomationEnabled True
CID 15
Type TYPE1
Instead of sending -Body $Body,I thought maybe I should just sent the values, as follows (which also failed) with the same message.
-Body $Body.Values
I did a heap of searching last night, but I am at a loss on how to convert that into a successful Invoke-WebRequest, and any help would be appreciated.

You are sending $Body as a Hashtable, try converting it to JSON
$Body = $Body | ConvertTo-Json
If you send $Body before the above line and again after to an echo service you'll see the difference
Invoke-RestMethod -Method 'POST' -Uri 'https://postman-echo.com/post' -ContentType 'application/json' -Body $Body

Related

"Unexpected token" or "JSON parse error - trailing comma" when doing API call with PowerShell

I have the following PowerShell API script:
$VMname = "abcd"
$IP_address = '2.2.2.2'
$url = "https://ansibletower.xyz.com/api/v2/job_templates/12321/launch/"
$token = "9998980fsfdfsdfdf"
$headers = #{Authorization = "Bearer $token"}
$contentType = "application/json"
$method = "POST"
#### $body = '{"extra_vars": {"vm_name": "abc", "vm_ip_address": "2.2.2.2"}}'
$body = '{"extra_vars":{"vm_name":'$VMname', "vm_ip_address":'$IP_address'}}'
Invoke-RestMethod -ContentType "$contentType" -Uri $url -Method $method -Headers $headers -Body $body
When I try it with manually predefined values in the body (see the commented body line above) - it works. But when I try it with variables $VMname and $IP_address, I get the error:
Unexpected token '$VMname', "vm_ip_address":'$IP_address'}}''
expression or statement.
And if I remove single quotes before and after variables VMname and IP_address I get:
{"detail":"JSON parse error - Expecting value: line 1
column...Possible cause: trailing comma."}
It is obviously a problem with syntax and / or formatting but I cannot find the solution. Does anyone know?
Use ConvertTo-Json for this:
$VMname = "abcd"
$IP_address = '2.2.2.2'
$body = ConvertTo-Json -Compress -Depth 9 #{
extra_vars = #{
vm_name = $VMname
vm_ip_address = $IP_address
}
}
$Body
{"extra_vars":{"vm_name":"abcd","vm_ip_address":"2.2.2.2"}}
Btw, it is probably not even required to serialize your body to Json as Invoke-Restmethod is supposed to take care of that:
-body
When the input is a POST request and the body is a String, the value to the left of the first equals sign (=) is set as a key in the form data and the remaining text is set as the value. To specify multiple keys, use an IDictionary object, such as a hash table, for the Body.

MSGraph - Invoke-WebRequest (403) Forbidden

I have a delegate App with Directory.ReadWrite.All permissions and a PS script to auth users over the app. It works when I use GET but I'm getting Forbidden when try PATCH method
Here's the part of that script:
$uri = "https://graph.microsoft.com/v1.0/devices/1111-2222-3333-4444-5555"
$method = "PATCH"
$body = '{
"extensionAttributes": {
"extensionAttribute2": "text"
}
}'
Invoke-WebRequest -Method $method -Uri $uri -Body $body -ContentType "application/json" -Headers #{Authorization = "Bearer $token"} -UseBasicParsing -ErrorAction Stop
Another thing: when using device ObjectID to construct Uri I'm getting the 403 Forbidden but if I use a $filter over a DeviceID I get 405 Method not allowed. Does it mean it doesn't like a filter and have to stick with the ObjectID? Is there a way when I run the GET with $filter to save in a variable only ObjectID within JSON query?
Thanks
sorted it, I needed Directory.AccessAsUser.All and used this to get the objectId variable:
$DsregCmdStatus = dsregcmd /status
if($DsregCmdStatus -match "DeviceId")
{
$DeviceId = $DsregCmdStatus -match "DeviceID"
$DeviceId = ($DeviceId.Split(":").trim())
$DeviceId = $DeviceId[1]
}
# Find Id
$uri = "https://graph.microsoft.com/v1.0/devices?`$filter=deviceId eq '$DeviceId'"
$method = "GET"
# Run Graph API query
$query = Invoke-WebRequest -Method $method -Uri $uri -ContentType "application/json" -Headers #{Authorization = "Bearer $token"} -UseBasicParsing -ErrorAction Stop
$output = ConvertFrom-Json $query.Content
$id = $output.value
$id = $id.id
Write-Host "Machine ID is $id"

How to use below JSON body in powershell for invoke-restmethod

I need generate token with "hostIDs" and "Fields" data. If I use only "hostIDs" it works but that token is invalid.
Below is the JSON body code which works with Postman but not in PowerShell.
{
"hostIds":[8876767,6736742,0986374],
"fields": ["ServiceTag","HardwareManufacturer","HardwareModel"]
}
Below JSON body works with Powershell only with 'hostIDs'. I also want to add another line to this body 'fields' which will fulfill the token generation. How to add multi-line?
$body = ConvertTo-Json #{
hostIds = 8876767,6736742,0986374
}
PowerShell code I am using for this API:-
#Credentials
$username = "xxxxxxx"
$password = "xxxxxxxxxxx"
$headers = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
#JSON Body
$body = ConvertTo-Json #{
hostIds = 8876767, 6736742,0986374
}
$EndPointURI = 'https://secure.logmein.com/public-api/v1/inventory/system/reports'
$response = Invoke-RestMethod -Uri $EndPointURI -Method Post -Headers #{Authorization=("Basic {0}" -f $headers)} -Body $body -ContentType 'application/json'
$token = $response.token
The syntax would be like this:
$body = ConvertTo-Json #{
hostIds = #('8876767', '6736742', '0986374')
fields = #('ServiceTag', 'HardwareManufacturer', 'HardwareModel')
}
Or like this:
$body = ConvertTo-Json #{
hostIds = #('8876767', '6736742', '0986374');
fields = #('ServiceTag', 'HardwareManufacturer', 'HardwareModel');
}

CURL script to powershell

I am trying to convert curl to powershell with Invoke-RestMethod for onesignal push
the script that is used for onesignal:
curl --include \
--request POST \
--header "Content-Type: application/json; charset=utf-8" \
--header "Authorization: Basic YOUR_REST_API_KEY" \
--data-binary "{\"app_id\": \"YOUR_APP_ID\",
\"contents\": {\"en\": \"English Message\"},
\"included_segments\": [\"Subscribed Users\"]}" \
https://onesignal.com/api/v1/notifications
I have tried with following example which I was using for pushover but without success.
$uri = "https://onesignal.com/api/v1/notifications"
$parameters = #{
app_id = 'YOUR_APP_ID'
contents = "en: English Message"
included_segments = 'Subscribed Users'
data = 'foo:bar'
}
$parameters | Invoke-RestMethod -Uri $uri -Method Post
I have used this powershell script for pushover which worked fine, but now I want to move to onesignal and I have problems with where/how to put rest api key inside with already using app_id to push messages forward to users.
The code is snatched from: https://documentation.onesignal.com/v5.0/reference#section-example-code-create-notification
I hope that someone can assist me with this problem.
Regards
Try the following
$basicAuth = "Basic REST_API_KEY";
$headers = #{ Authorization = $basicAuth };
$uri = "https://onesignal.com/api/v1/notifications";
$body = #{ app_id = 'YOUR_APP_ID'; contents = #{ en = 'English Message' }; included_segments = #('Subscribed Users'); data = #{ foo = 'bar' }} | ConvertTo-Json;
Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -ContentType "application/json; charset=utf-8" -Body ([System.Text.Encoding]::UTF8.GetBytes($body));
I have no idea how this endpoint works but it should work something like this:
$key = "Basic RESTAPIKEY"
$headers = #{}
$headers.Add("Authorization",$Key)
$headers.Add("Content-Type","application/json; charset=utf-8")
$uri = "https://onesignal.com/api/v1/notifications"
$parameters = #{
app_id = 'YOUR_APP_ID'
contents = "en: English Message"
included_segments = 'Subscribed Users'
data = 'foo:bar'
} | ConvertTo-Json
Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($parameters)) -ContentType "application/json"
If the data-binary part is sent with PowerShell you could also use the -InFile parameter from Invoke-RestMethod.
If just read a little at the link you posted and think the body part should be like the following:
$parameters = #{
app_id = "5eb5a37e-b458-11e3-ac11-000c2940e62c"
included_segments = "Array of active users"
data = #{
foo = "bar"
}
contents = #{
en = "English Message"
}
} | ConvertTo-JSON

Manipulate json and send it into web request using Powershell

I'm trying to manipulate a json object and send it as content into the body of a put / post web request. The source of my json is a file on my disk.
This is my Powershell script:
$urlBase = 'https://mysite.myapp.com/service/api/Item/'
$myJson = (Get-Content 'file.json' | ConvertFrom-JSON)
# Then I manipulate my object
$id = $myJson.id
$myJson.version = '1.2.3.4'
# Request
$response = Invoke-RestMethod -Uri ($urlBase + $id) -Method Put -Body $myJson -ContentType 'application/json' -Headers $hdrs
When I execute my script y get this error message:
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:18 char:17
+ ... $response = Invoke-RestMethod -Uri ($urlBase + $id) -Method Put -Body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
If I change my $myJson asignment for this the request works fine...
$myJson = Get-Content 'file.json'
... , but then I can't manipulate my json before send it.
Edited:
If I try to convert back using ConvertTo-Json I get the same error:
$convertedBack = $myJson | ConvertTo-Json
# Request
$response = Invoke-RestMethod -Uri ($urlBase + $id) -Method Put -Body $convertedBack -ContentType 'application/json' -Headers $hdrs
As pointed out in the comments: you need to convert your object back to JSON using the ConvertTo-Json cmdlet.
I see that you've tried that now and had the same problem. So I ask you this: is the value of $convertedBack exactly what you expected? Dump it to file and check!
The reason I am suspicious of this detail is that ConvertTo-Json has a little gotcha in it. Specifically the -Depth parameter which can cause some data loss.
-Depth
Specifies how many levels of contained objects are included in the JSON representation. The default value is 2.
Example Without -Depth
$basicJsonObject = #"
{
"name": "George",
"properties": {
"mood": "jovial",
"coffee": {
"hasCoffee": true,
"mugContents": {
"milk": false,
"doubleShot": true
}
}
}
}
"#
$psObject = ConvertFrom-Json -InputObject $basicJsonObject
Write-Host "Freshly Imported"
Write-Host "DoubleShot = $($psObject.properties.coffee.mugContents.doubleShot)"
$convertedBack = ConvertTo-Json -InputObject $psObject
$reConverted = ConvertFrom-Json -InputObject $convertedBack
Write-Host "Re-Converted"
Write-Host "DoubleShot = $($reConverted.properties.coffee.mugContents.doubleShot)"
Results
Freshly Imported
DoubleShot = True
Re-Converted
DoubleShot =
Example With -Depth
Change one line of code:
$convertedBack = ConvertTo-Json -InputObject $psObject -Depth 5
Results
Freshly Imported
DoubleShot = True
Re-Converted
DoubleShot = True
Note how the new results include the value from the $reConverted variable. This is because the data is not lost further upstream!