I currently have a Powershell script to fetch orders through a REST API Call.
The script (1) Uses the INVOKE-ResetMethod for the API call, then (2) uses the Export-CSV cmdlet to save the results in a CSV file, which works fine:
$uri = "https://api.SomeWebisite.com/api/orders?limit=1000"
$headers = #{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer SomeTokenKey'
'Accept'= 'application/json'
}
Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body $body | select -expand data | select * | export-csv c:\extracts\orders.csv -notype -Force
The problem is, the provider has a 1000 record return limit, which doesn't work for us, as we need all orders in the dataset.
What would be the most efficient/appropriate way to retrieve the full recordset & and have all orders reside in a single CSV file?
Related
I want to add an Azure-DevOps query to a pipeline,
Is there an option to put a query in a yaml file?
If yes, how?
Thanks in advance.
I suppose that you could run a rest api for query via powershell script or something like that in a pipeline task to do it.
GET https://dev.azure.com/{organization}/{project}/_apis/wit/queries/{query}?api-version=7.0
Powershell Script
# Define organization base url, PAT and API version variables
$orgUrl = "https://dev.azure.com/{org}/{project}"
$pat = "{pat}"
$queryString = "api-version=7.0"
# Create header with PAT
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))
$header = #{authorization = "Basic $token"}
# Get the list of all projects in the organization
$projectsUrl = "$orgUrl/_apis/wit/queries/{query}?$queryString"
$result = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -Headers $header | ConvertTo-Json | ConvertFrom-Json
write-host $result
If you are going to echo the specific element from the query result, you could modify the $result line with below
$result = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -Headers $header | ConvertTo-Json | ConvertFrom-Json | Select-Object -ExpandProperty {your element}
write-host $result
You could also create a query with rest api, and with the similar powershell script to put into pipeline.
POST https://dev.azure.com/{organization}/{project}/_apis/wit/queries/{query}?api-version=7.0
I'm currently use the following Powershell script as an API call using REST to retrieve table data from a vendor, which appears to be in JSON format:
$uri = "https://www.SomeWebsite.com/api/orders"
$headers = #{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer <MyTokenID>'
'Accept'= 'application/json'
}
Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body $body
The script works, and output as such in Powershell:
data
----
{#{type=appointments; id=1234; attributes=; links=; relationships=}, #{type=appointments; id=1235; attributes=; links=; relationships=}, #{type=appointments; i...
I need the ability to export this as a CSV file.
How can I incorporate anexport-CSV CMDLET (something like below) to have it work with the above syntax? (Preferably ALL Columns/Headers)
Select ID, Status | Export-Csv -Path "filename.csv" -NoTypeInformation
Your Invoke-RestMethod outputs an object with a data property. The data property contains the objects you want to reference. So you must expand that property first.
Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body $body |
Select-Object -ExpandProperty data |
Select-Object id,status |
Export-Csv filename.csv -NoTypeInformation
I have a powershell script, which is calling a third party API and getting some response data back. I'd like to just log the response object to see what exactly is returning. I've tried
Write-Host ($item | Format-List | Out-String)
But that doesn't seem to be working. The API says it will return JSON but I'm not sure how to verify the return at all.
Full script is something like this.
$queryURL = "xyz"
$apiResponse = Invoke-RestMethod -Uri $queryURL -Method Get -ContentType "application/json" -Headers $header
You can pipe the Invoke-RestMethod to the ConvertFrom-Json cmdlet, this will convert the output into an object you can easily query:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7.1
$j = Invoke-WebRequest 'https://api.github.com/repos/PowerShell/PowerShell/issues' | ConvertFrom-Json
Calling $j will output the object created from the JSON.
i am looking for a way to get all elements of a table with around 1000 entries
i am able to get 10 elements that are "visible" on the website itself, but all the others are on more pages, which i cant read out with the invoke-restmethod command.
Is there a way to get all entries of the table?
# GET USERPROFILES
$basicAuthValue = "token $token"
$Headers1 = #{
Authorization = $basicAuthValue
}
$response2 = Invoke-RestMethod 'https://Url-that-im-using' -Method 'GET' -Headers $headers1 -Body $body
$response2 | ConvertTo-Csv
I am using Invoke-RestMethod to make a post request to create a new document in CosmosDB. So far I followed theses links: Invoking Rest API using PowerShell - CosmosDb
, Add a Document to CosmosDB via the REST API using PowerShell and the official documentation.
My script retrieves an object like this from external service:
{
"ObjectId": "bd33f6b5-a066-4f0f-8d1b-291a6a2b90ba",
"Date": "\/Date(1589379850000)\/",
"Data": "{\"CreationTime\":\"2019-06-13T13:21:55\",\"Id\":\"e985f142-9359-4ebf-a319-7fa30b6c9987\", \"Fields\":[{\"Name\":\"foo\",\"Value\":\"bar\"}]}"
}
My objective is to post the field Data into cosmos. To this I extract this field using: $payload | Select-Object -expand Data. (I have the above json as PowerShell object). Since this extracted object is a string, I passed it to Invoke-RestMethod:
Invoke-RestMethod -Method $Verb -Uri $queryUri -Headers $headers -Body $payload
But I keeping getting a Bad Request status. I've also tried the following:
Invoke-RestMethod -Method $Verb -Uri $queryUri -Headers $headers -Body ($payload | ConvertTo-Json -Depth 100)
Invoke-RestMethod -Method $Verb -Uri $queryUri -Headers $headers -Body ($payload | ConvertFrom-Json | ConvertTo-Json -Depth 100)
Note: I was able to deserialize this string in C# using newtonsoft (I made a function to receive the PowerShell request). I also was able to insert this document via Postman. Looks like the issue is in the body of the request, since using the PowerShell code generated by Postman worked for me.
Edit: I am passing the content type ("application/json") in header of the request. It also fails when pass it I directly to the Invoke-RestMethod
Can someone give a light? To me it should work fine.
Your payload is missing the required id attribute:
Remember that the REST API is for the Core SQL API operations. Your payload seems to be for a Mongo document?
Make sure you are also passing the partition key. Reference: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/PowerShellRestApi/PowerShellScripts/CreateItem.ps1
The data seems to be double converted, to extract it you can use this:
$json = '{"ObjectId": "bd33f6b5-a066-4f0f-8d1b-291a6a2b90ba", "Date": "\/Date(1589379850000)\/", "Data": "{\"CreationTime\":\"2019-06-13T13:21:55\",\"Id\":\"e985f142-9359-4ebf-a319-7fa30b6c9987\", \"Fields\":[{\"Name\":\"foo\",\"Value\":\"bar\"}]}"}'
$data = $json | ConvertFrom-Json | Select-Object -ExpandProperty "Data" | ConvertFrom-Json
$body = $data | ConvertTo-Json -Depth 10
So now $body is a proper JSON object which can be send by the Invoke-RestMethod:
Invoke-RestMethod -Method $verb -Uri $queryUri -Headers $headers -Body $payload -ContentType "application/json"