Thursday, February 8, 2018

How to handle "429" error in PowerShell

Sometimes we got error "The remote server returned an error: (429) Too Many Requests", when accessing SharePoint Online through PowerShell script.

Below is how I handle it:

$Global:_retryCount = 1000
$Global:_retryInterval = 10

for($retryAttempts=0; $retryAttempts -lt $Global:_retryCount; $retryAttempts++){
Try{
$ctx.ExecuteQuery()
break
}
Catch [system.exception]{
Start-Sleep -s $Global:_retryInterval
}
}

6 comments:

  1. Does this actually work though?

    I would imagine that after the first ExecuteQuery, the context would be lost and the retries wouldn't actually do anything.

    ReplyDelete
    Replies
    1. It works well during my test. The content of $ctx is stored at client side.

      Delete
  2. I just encountered this issue. Might help me out! Will try and let you know how it goes!

    ReplyDelete
  3. Works great. Thanks

    ReplyDelete
  4. I had this crop up recently as well when running a script to disable onedrive sync for all users in a tenant. This variation slowly increases the wait time for each request and gives up after 8 attempts.

    $retrySecs=1
    $retryMax=8
    $ErrorActionPreference = "Stop"

    for($retryAttempts=0; $retryAttempts -lt $retryMax; $retryAttempts++){
    Try{
    Set-SPOUser -Site $onedrive.url -LoginName $SiteCollAdmin -IsSiteCollectionAdmin $true
    break
    } Catch [system.exception]{
    Start-Sleep -s ($retryAttempts*$retrySecs)
    }
    }

    ReplyDelete