Using PowerShell Invoke-RestMethod to query a REST and getting back a response as shown below:
REST> Write-Host $response
#{Success=}
REST> Write-Host $response.Success
#{count=1; uri=https://useful}
I would like to be able to assign uri to a variable.
You can access the uri using:
$yourVariable = $response.Success.uri
Related
I am running this code and its not returning the data to my $response variable as I expected.
$targetUrl = "https://www.tigerrunresort.com/vacation-rentals-homes-search.asp?txtStartDate=8%2F1%2F2022&txtEndDate=8%2F7%2F2022&categoryid=11317&destinationID=0&communityID=0&amenBedrooms=-1&maxPersons=0&advs=&sort=0¤tPage=1&flexibleDates=0&flexibleDatesThreshold=&searchAmenities=&showOnlySpecials=0"
$Response = Invoke-WebRequest -Uri $targetUrl
In rawcontent I should expect to see the string "Sorry" which does not show. What do I need to do to get it to return the values as I would see interactively?
Thanks!
You are not seeing it because it is not actually in the response. It is probably created and appended to the DOM by Javascript. Due to the Invoke-WebRequest not executing Javascript, it is never part of the response. If you are looking to orchestrate browsers (which do execute Javascript and offer access to the DOM) maybe you can try taking a look at Selenium (https://www.selenium.dev/)
For lauching a web request you can use for example start-process like this : start-process microsoft-edge:http://google.com/
I have a release pipeline in Azure DevOps.
I have added an Agentless job running that calls a REST API endpoint.
At the moment this works as expected.
However my next challenge is to take the response from the API call and perform some tasks. Here is a breakdown of the logic I am trying to achieve:
GET list of students (Connect to REST API with GET)
Read response from above step and look for a specific value
If value exists continue
If value does not exist then POST new value to REST API endpoint
Like I said, I have step 1 sorted but I do not know how to add the if-then-else logic
Any help appreciated
Thanks
You need to look at the output of your response to get your specific value.
$reponse = Invoke-RestMethod -Method GET -URI $url
$specificValue = $response.output.specificValue
if(!$specificValue) {
##value does not exist
Invoke-RestMethod -Method POST -URI $url -Body $body
}
I am trying to use Invoke-Restmethod in Powershell to call an API (I'm fairly new to this). I can get it to POST and return the jwt access token. I can also use that token to return an id via GET, however I'm then having trouble with the next step of returning the next set of data. I can get it to work manually via curl. I believe the issue may be because multiple headers are required to return the tenant list and I'm unsure of the format to get this to work.
The curl script looks as follows, and works as expected:
curl -XGET -H "Authorization: Bearer <jwt access token>" -H "ID: <id>" https://theapiurl.com/.......
I've tried multiple ways to do this in powershell, most recently as below, but nothing I'm trying works. I've tried returning the individual $headers contents and building a string (i.e. $headers2 = $.headers.Item(Authorization) + ......) but that doesn't work either. To be honest, I've tried so many different things I've forgotten what I have and haven't tried
$headers = #{
'ID' = $id
'Authorization' = $auth_string
}
$response = Invoke-RestMethod -Method Get -Headers $headers -Uri $url
Please could you let me know the correct way to add multiple headers (which I think is the problem and what I'm getting wrong)?
In case it's useful to anyone else, another syntax for setting the parameters of this commandlet is as follows (real working example for uploading to the GitHub release repository). It's usful to set all the switches (without prepending a hyphen) in the parameters object like so:
$upload_params = #{
Uri = $upload_uri + "?name=$asset_name"
Method = 'POST'
Headers = #{
'Authorization' = "token $github_token"
'Accept' = 'application/vnd.github.everest-preview+json'
}
InFile = $asset
ContentType = 'application/octet-stream'
}
"Uploading $asset..."
$upload = Invoke-RestMethod #upload_params
"The server returned:"
echo $upload
The variable $upload contains the full object returned from the server (converted from json to a PowerShell object). So, for example, you can also get properties of this obect like so:
"Upload successfully posted as " + $upload.url
Thanks for all the responses - none of them were really the answer but they did give me the confidence I was doing it the right way.
I'd been using PS Write-Host to check the data returned - this was working for the tokens and ID's, but wasn't working for next step. I wasn't getting an error, just no data. (I did see the returned data when testing manually in a command prompt window).
As soon as I added an -OutFile to the PS and checked the file, I realised it was working all along and PS just wasn't showing me the results. 2 hours wasted, although I've learnt more as a result!
I have an MS Graph API in PowerShell working for the most part.
I am using
$Uri = $null
$Uri = "https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode"
$payload=$null
$payload = Invoke-RestMethod -uri $Uri -Headers $Header -Method Get -ContentType "application/json"
$payload.value
however, it is not changing the field selection. It keeps returning the default fields as demonstrated here
https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http
What could I possibly be doing wrong?
I am using application based authentication. The payload is being returned but it is not recognizing the $select statement.
No errors are being returned by the PowerShell
I run it in Graph Explorer it works fine.
The error is caused by the type of string declaration used for the Uri string. You are declaring the string like this:
$Uri = "https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode"
This tells Powershell, that you want to evaluate the string. $ is Powershell's variable identifier. Undeclared variables are set automatically to an empty string, when evaluated in a string. Therefore the request executed against the Graph Api is:
https://graph.microsoft.com/v1.0/users?=displayName,givenName,postalCode
Your can check this yourself by writing the variable to the host:
Write-Host $Uri
If you execute this query with the Graph Explorer. It will return all users without an applied filter, which is the behaviour you have observed. You need to change the declaration to:
$Uri = 'https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode'
Then, Powershell will not interpret $select as a variable and your request should work properly.
I'm publishing a custom metric called "ConnectionCount" on my AppInsights component.
I'm looking for a way to query the last values for that metric in a PowerShell script.
I've used this command in the past:
$resourceId = "/subscriptions/$subscriptionId/resourceGroups/MyResourceGroup/providers/microsoft.insights/components/MyAppInsight"
Get-AzureRmMetric -ResourceId $resourceId -TimeGrain $timeGrain -StartTime $startTime -MetricNames $metric`
But when run this, I just get an error back:
Get-AzureRmMetric : Operation returned an invalid status code 'NotFound'
I also tried the REST API, using this code:
$resourceGroupId = "subscriptions/$subscriptionId/resourceGroups/$resourceGroupName"
$filter = "(name.value eq 'ConnectionCount') and timeGrain eq duration'PT5M' and " +
"startTime eq 2017-07-20T17:00:47.8791884Z and endTime eq 2017-07-25T17:30:47.8832111Z"
$apiVersion = "2015-05-01"
$uri = "https://management.azure.com/$resourceGroupId/providers/microsoft.insights/components/$appInsightName/metrics?api-version=$apiVersion&`$filter=$filter"
Invoke-RestMethod $uri -Headers $headers
But it simply gives me back an empty response.
Does anyone know if something has changed in the Metrics API that would prevent custom metrics values to be retrieve from PowerShell or the REST API?
if you're getting an empty response (and not any kind of failure) then i'd suggest verifying that your query is doing what you expect and not filtering out all your data?
your filter looks really suspicious to me, this part timeGrain eq duration'PT5M' seems like an odd filter to me. normally you'd specify the range and the grain as other parts of the query, not inside the $filter part
i'd suggest verifying all of your REST stuff with the API docs at https://dev.applicationinsights.io, and try testing your query at
https://dev.applicationinsights.io/apiexplorer/metrics