SpanningCloudApps / SB365-Powershell

Spanning Backup for Office 365 REST API PowerShell Module
https://spanning.com
Apache License 2.0
10 stars 5 forks source link

Invoke-WebRequest : Too many requests #40

Closed TRipka07 closed 2 years ago

TRipka07 commented 3 years ago

Hello,

We have a PowerShell script to license Spanning users who are members of an AD group and are experiencing this error when the script runs.

TerminatingError(Invoke-WebRequest): "Too many requests, please try again later." Invoke-WebRequest : Too many requests, please try again later.

How can we resolve this?

Thanks, Tanner

SPMatthewMcD commented 3 years ago

Can you try the sample here: https://github.com/SpanningCloudApps/SB365-Powershell/blob/master/samples.md#license-an-azure-ad-group We have a new "bulk" endpoint that enables up to 500 users in one request.

TRipka07 commented 3 years ago

Hello,

I've tested with your suggestion. Here is an example of my script:

$userList = [System.Collections.ArrayList]@()

Get-ADGroup -Identity "Spanning Backup Users" | Get-ADGroupMember | ` foreach {$userList.Add($_.UserPrincipalName)}

Submit the list to Spanning

Enable-SpanningUserList -UserPrincipalNames $userList

Error message I'm receiving:

TerminatingError(Enable-SpanningUserList): "Cannot validate argument on parameter 'UserPrincipalNames'. The number of provided arguments, (1295), exceeds the maximum number of allowed arguments (500). Provide fewer than 500 arguments, and then try the command again." Enable-SpanningUserList : Cannot validate argument on parameter 'UserPrincipalNames'. The number of provided arguments, (1295), exceeds the maximum number of allowed arguments (500). Provide fewer than 500 arguments, and then try the command again.

SPMatthewMcD commented 3 years ago

Can you try grouping them into 500 item lots? Something like this:

#Break List into batches of 500
    $group = 500
    $i = 0
    $userBatch = @()
    $results = @()
    do {
        #Send the batch of Users 500 at a time.
        $userBatch = $userList[$i..(($i+= $group) - 1)]
        if ($pscmdlet.ShouldProcess("Processing $($userBatch.Count) users", "Enable-SpanningUserList")){
            $resultsBatch = Enable-SpanningUserList -AuthInfo $AuthInfo -UserPrincipalNames $userBatch

            #Need to join the results
            $results += $resultsBatch
        }
    }
    until ($i -gt $userList.count -1)
TRipka07 commented 3 years ago

I've added the batch suggestion and I'm receiving this error.

Enable-SpanningUserList : Cannot validate argument on parameter 'UserPrincipalNames'. The number of provided arguments, (1297), exceeds the maximum number of allowed arguments (500). Provide fewer than 500 arguments, and then try the command again. At C:\Scripts\SpanningBackup\Enable-SpanningUser_v2.ps1:44 char:45

Here is the script with the batch inserted and I may something out of order. Sorry, my powershell skills are not the best.

`#Connect to Spanning Get-SpanningAuthentication -ApiToken $Password -Region US -AdminEmail spanningadmin@domain.com

Assign licenses to users

Create an ArrayList

$userList = [System.Collections.ArrayList]@()

Break List into batches of 500

$group = 500
$i = 0
$userBatch = @()
$results = @()
do {
    #Send the batch of Users 500 at a time.
    $userBatch = $userList[$i..(($i+= $group) - 1)]
    if ($pscmdlet.ShouldProcess("Processing $($userBatch.Count) users", "Enable-SpanningUserList")){
        $resultsBatch = Enable-SpanningUserList -AuthInfo $AuthInfo -UserPrincipalNames $userBatch

        #Need to join the results
        $results += $resultsBatch
    }
}
until ($i -gt $userList.count -1)

Get-ADGroupMember -Identity "Spanning Backup Users" | Get-ADUser -Properties userPrincipalName | ` foreach {$userList.Add($_.UserPrincipalName)}

Submit the list to Spanning

Enable-SpanningUserList -UserPrincipalNames $userList `

SPMatthewMcD commented 3 years ago

Are you certain your array has fewer than 500 items? The error indicates that you are still passing too much data.

SPMatthewMcD commented 2 years ago

I am going to write up a sample for this. Closing for now.