简体   繁体   中英

Running multiple iteration API calls using Powershell

I need to run 1000 API calls for 1000 different URI's. I tried to run 1000 different URI in one go as shown below but the API has a limit of 1000 lines. Is there a way to run this code 1000 times producing 1000 separate JSON files or in one JSON file without me having to run the URI with the powershell script indivdiually.

$access_token ="dfdfgdfgf45h5676rdffghfghfbazbfgjvdrtwsffrgr"
$LogPath ='C:\StackOVerflow'

$URI = 'https://api.web.ser/audits/a_80weqwepeopeoA'
       'https://api.web.ser/audits/a_B0980kdfl;skfd'
       'https://api.web.ser/audits/a_Csdfsdfsjlksdc'
       'https://api.web.ser/audits/a_Tlksdjfalkjdff'
       'https://api.web.ser/audits/a_Fldkfjlsfjdfdl'
       'https://api.web.ser/audits/a_Fsdfsdfsdfsd34'
       'https://api.web.ser/audits/a_G34gfgf4ffsdrf'
       'https://api.web.ser/audits/a_Haere34534fdgf'
       'https://api.web.ser/audits/a_Ifdl023lererew'
       'https://api.web.ser/audits/a_Afrererl45645w'

$headers = @{“authorization” = “Bearer $access_token”}

$result = Invoke-RestMethod -Uri $URI -Headers $headers |ConvertTo-Json| out-file "$LogPath\id_ExtractLogs$(((get-date).ToString("yyyyMMdd"))).json"


$result

You cannot pass an array of uri to the Invoke-RestMethod cmdlet; they must be iterated.

Here's a working solution:

$uri = 'https://api.web.ser/audits'
$reportList = @(
    'a_80weqwepeopeoA'
    'a_B0980kdfl;skfd'
    # .. etc.
)

$token = 'secret'
$headers = @{ Headers = @{ Authorization = "Bearer $token" }}

$logPath = 'C:\StackOverflow'

foreach ($report in $reportList) {
    $stamp = Get-Date -Format FileDateTime
    Invoke-RestMethod -Uri "$uri/$report" -OutFile "$logPath\id_$stamp.json" @headers
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM