0

I am working on a requirement where I have to check if the api call needs to be looped over or not. I am using the below code to accomplish this requirement. If I take out the if else block and write for either loop no loop things work as expected.

PoSh:

    $Loop = "1" # 0 for no looping 1 for looping
    
    if ($Loop -eq 1) {
    
    $Header = @{
        "authorization" = "Bearer $token"
    }
    #make REST API call
    $Parameters = @{
        Method      = "GET"
        Headers     = $Header
        ContentType = "application/json"
        Body        = $BodyJson
    }
    $startYear = 2014
    $endYear = 2022
    
    $Data = {for($year=$startYear; $i -le $endYear; $year=$year+1) {Invoke-RestMethod -Uri "https://api.mysite.com/v1/data/year/Year/" + [string]$year @Parameters -DisableKeepAlive -ErrorAction Stop}} | ConvertTo-Json
    
    }
    
    else {Write-Output "No loop"
    $Header = @{
        "authorization" = "Bearer $token"
    }
    #make REST API call
    $Parameters = @{
        Method      = "GET"
        Headers     = $Header
        ContentType = "application/json"
        Body        = $BodyJson
    }
    
    $Data = Invoke-RestMethod -Uri "https://api.mysite.com/v1/data" @Parameters -DisableKeepAlive -ErrorAction Stop | ConvertTo-Json

}

Error:

Cannot bind parameter because parameter 'Uri' is specified more than once. To provide multiple values to parameters that can accept multiple values, use the array syntax.
5
  • "https://api.mysite.com/v1/data" + [string]$year should be ("https://api.mysite.com/v1/data" + [string]$year). While in a scriptblock, the commands won't execute either, not sure if that's your intentions. You ConvertTo-Json is outside your else statement as well. Commented Aug 30, 2022 at 17:50
  • That was a typo in the OP. I modified it. Commented Aug 30, 2022 at 17:57
  • same goes for the $data in your if statement. The error should narrow down the line that's giving you that exception. Guessing it's the one in your for loop but, not sure if that's a typo as well Commented Aug 30, 2022 at 18:02
  • For some reason they work fine when independently but when I put it together in the if else block that's when it runs into error. Commented Aug 30, 2022 at 18:29
  • 1
    More typo's... You define a Hashtable for splatting in variable $Parameters, but on the Invoke-RestMethod you use @params. The for loop is wrong too, You define a looping variable $year, but then you use $i -le $endYear, so an undefined variable $i instead of $year. Also, since for both cases the $Header and $Parameters Hashtable are the same, why not define that only once above the if..else construct? and lastly, why not simply use -Uri "https://api.mysite.com/v1/data/year/Year/$year" ?? Commented Aug 30, 2022 at 19:17

1 Answer 1

1

I have of course no idea what your https://api.mysite.com/v1/data would return and if it is actually needed to convert the returned data to Json at all, but continuing from my comments, try

# make this a Boolean value for clarity
$Loop = $true   # $false for no looping $true for looping


# splatting Hashtable for REST API call
$Parameters = @{
    Method           = "GET"
    Headers          = @{ "Authorization" = "Bearer $token" }
    ContentType      = "application/json"
    Body             = $BodyJson
    # you can incorporate these parameters as well
    DisableKeepAlive = $true
    ErrorAction      = 'Stop'
}

if ($Loop) {
    Write-Host "Start looping.."
    $startYear = 2014
    $endYear   = 2022
    # use the $() subexpression to combine the various outputs and convert that to Json
    $Data = $(for ($year = $startYear; $year -le $endYear; $year++) {
        Invoke-RestMethod -Uri "https://api.mysite.com/v1/data/year/Year/$year" @Parameters
    }) | ConvertTo-Json
}
else {
    Write-Host "No loop"
    $Data = Invoke-RestMethod -Uri "https://api.mysite.com/v1/data" @Parameters | ConvertTo-Json
}

P.S. The error you saw in your code was caused by the wrong variable you used in the for loop with $i -le $endYear instead of $year -le $endYear. That and the fact that you put the whole loop inside a scriptblock made variables $startYear and $endYear invisible..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.