I have a powershell script and I am trying to assign the response of the GET method to value $a. Bellow I have my code but it does not work. How can I assign the response to this value?
Thank you
$a = Invoke-RestMethod -Uri "https://cloud.skytap.com/templates/555949" -Method GET -ContentType "application/json" -Headers $headers | out-null
Write-Host "$a"
You're sending your code to out-null, which removes the output!
Remove | Out-Null and you'll find that your assignment to $a is working. Or perhaps you'll find an error message instead. If so, let me know and I'll do my best to help you.
To clarify, you should be running this instead.
$a = Invoke-RestMethod -Uri "https://cloud.skytap.com/templates/555949" -Method GET -ContentType "application/json" -Headers $headers
Related
I am currently having issues creating a powershell script that taks to an api with Invoke-RestMethod and a loop, I have spent all day trying to figure out where i am going wrong but i have not managed to come up with something.
Here is the code that i am trying to make
$url = "/api/Rest/v1"
$Body = #{
Username = ""
Password = ""
Privatekey = ""
}
$apikey = Invoke-RestMethod -Method 'Post' -Uri $url/Authenticate -Body $body
$headers = #{
'Authorization' = $apikey
}
$allusers = Invoke-RestMethod -Uri $url/Users -Method Get -Headers $headers | ft -HideTableHeaders Id
foreach ($userid in $allusers)
{
echo $userid
Invoke-RestMethod -Uri $url/Users/$userid -Method Get -Headers $headers
echo "test"
}
I am not having issues with the veriables $apikey and $allusers as they seem to output what i need but i think my issue is to do with the outbut being in format table but i have tried other methods for the for each and i have no clue where i am going wrong
So i have tested the Invoke-RestMethod commands on there own and they work as exspected but when i try the script above i get the following.
Invoke-RestMethod : {"Message":"User with specified id was not found."}
the output of $allusers displays something like the following for the user ID
dce502ed-e4b6-4b5e-a047-0bf3b34e98c6
dc1e60c1-99a7-479a-a7d6-0dc618c8dd5e
1bd98bb0-a9ee-46b5-8e2e-0e3146aab6b3
AKA the following work with no issues and outputs what i need
Invoke-RestMethod -Uri $url/Users/1bd98bb0-a9ee-46b5-8e2e-0e3146aab6b3 -Method Get -Headers $headers
I would really appreciate some kind of guidance on this.
The standard advice applies:
Format-* cmdlets (such as Format-Table, whose built-in alias is ft) emit output objects whose sole purpose is to provide formatting instructions to PowerShell's for-display output-formatting system.
In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing - see this answer for more information.
Therefore, remove the | ft -HideTableHeaders Id pipeline segment and use member-access enumeration to extract all .Id property values as data.
$allusers = (Invoke-RestMethod -Uri $url/Users -Method Get -Headers $headers).Id
I am trying to process a multipart GET call in powershell and then save the zipfile it contains to disk. I can execute this:
$response = Invoke-RestMethod -Uri $reqUrl -Method Get -Headers $headers
and then echo out the filename and contents. In order to save the file, I tried this:
$response = Invoke-RestMethod -Uri $reqUrl -Method Get -Headers $headers -ContentType "multipart/form-data" -Outfile result.zip
This raises an error (Invalid Operation). So I tried this:
$response = Invoke-RestMethod -Uri $reqUrl -Method Get -Headers $headers -ContentType "application/zip" -Outfile result.zip
This creates a file called result.zip which isn't valid. I know that the response is multipart, so while this doesn't raise an error, I am not surprised that the file is invalid because it must contain all of the parts.
I have looked around, but all I find are ways of using Invoke-RestMethod to POST mulitpart content.
This is the error when I try to open the resulting zip file:
I have also tried to decode the result as below, but with the same results.
$B64String = $response.resultObject
Write-Host "resultObject size: $([Math]::Round($B64String.Length / 1Mb,2)) MiB"
$swDecode = [System.Diagnostics.Stopwatch]::StartNew()
$bytes = [System.Convert]::FromBase64String($B64String)
$swDecode.Stop()
Write-Host "Decoded size: $([Math]::Round($bytes.Count / 1Mb,2)) MiB"
Write-Output $bytes > $($response.fileName)
I found the answer
$response = Invoke-RestMethod -Uri $reqUrl -Method Get -Headers $headers
$response | ConvertTo-Json
[IO.File]::WriteAllBytes($response.fileName, [System.Convert]::FromBase64String($response.resultObject))
I am attempting to use Powershell to perform some "POST" requests. However, I can't seem to get the JSON correctly formatted. How do I accomplish this?
>> $JSON=#{name:"TestName"}
>> Invoke-WebRequest -Uri http://localhost:7071/api/HttpTrigger1 -Method POST -Body $JSON
>> $response = Invoke-WebRequest -Uri "http://localhost:7071/api/HttpTrigger1" -Method Post -Body $JSON -ContentType "application/json"
ParserError:
Line |
1 | $JSON=#{name:"TestName"}
| ~
| Missing '=' operator after key in hash literal.
So, there are two ways you can do this:
The first, as suggested by Santiago, is
$json = '{name:"TestName"}'
$response = Invoke-WebRequest -Uri "http://localhost:7071/api/HttpTrigger1" `
-Method Post -Body $json -ContentType "application/json"
The second, using (roughly) the syntax you were using, is
#create a Powershell object (note the use of '=' instead of ':' for assignment)
#(such a simple example is not an improvement over the example above)
$json = #{ name = "TestName" } | ConvertTo-JSON
$response = Invoke-WebRequest -Uri "http://localhost:7071/api/HttpTrigger1" `
-Method Post -Body $json -ContentType "application/json"
The first method is certainly cleaner and more direct. The second is useful when the source data for the request comes as the result of manipulating Powershell objects, and you want to convert them for use in a web request.
Here is the GET request I wrote in PowerShell:
$registry = Invoke-WebRequest -Uri "https://${web_ip}/v1/registry/" -Method GET -Headers #{Authorization="token $token"} -ContentType "application/json"
Write-Host $registry
It will show something like this:
[{"user": "corey", "project": "corey", "registry": "corey-registry"}]
I tried to parse the response to get the value from the key "registry", but it didn't work as my expectation.
# to get the first value in the list
$registry[0] => the output is the same as listed above
# check the type
$registry.GetType() => Microsoft.PowerShell.Commands.HtmlWebResponseObject
I don't know how to convert HtmlWebResponseObject to a json or list object, and I have no idea how to get the value "corey-registry" in code either, that's my main problem.
I stuck at this issue, any ideas? I would appreciate any help.
The response has the Content property which contains the raw JSON. Use ConvertFrom-Json to convert it into an object. You can then easily access the registry property.
Here is a (quite verbose) example with some explanations:
# get response
$response = Invoke-WebRequest -Uri "https://${web_ip}/v1/registry/" -Method GET -Headers #{Authorization="token $token"} -ContentType "application/json"
# get raw JSON
$json = $response.Content
# deserialize to object
$obj = ConvertFrom-Json $json
# you can now easily access the properties
$registry = $obj.registry
I'm trying to query this API https://docs.iocparser.com/api-reference/parse-api
I'm getting a failure to resolve URL error, which makes me believe that somewhere in this line I've gone wrong with the formatting, but I can't figure out where, so any help would be appreciated.
$Request = Invoke-RestMethod -Method Post
-Uri 'https://api.iocparser.com/url'
-Headers #{"Content-Type" = "application/json"}
-Body #{'url' = 'https://pastebin.com/raw/rgnvuYi2'}
-Verbose
This is the error I'm getting back.
Invoke-RestMethod : {"status": "error", "error": "IOC Parser failed to resolve
the given URL"}
Explicitly convert the Body data to a JSON string. I do remember Invoke-RestMethod doing it automatically before, but in this case, it is not.
Invoke-RestMethod -Method Post -Uri 'https://api.iocparser.com/url' -Headers #{"Content-Type" = "application/json"} -Body (#{'url' = 'https://pastebin.com/raw/rgnvuYi2'} | ConvertTo-Json) -Verbose