当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 批量执行 CSOM PowerShell 脚本以避免 SharePoint Online 中的 429 资源限制问题

作者:精品下载站 日期:2024-12-14 21:32:35 浏览:12 分类:玩电脑

批量执行 CSOM PowerShell 脚本以避免 SharePoint Online 中的 429 资源限制问题


问题:通过 CSOM PowerShell 脚本执行批量操作时,SharePoint Online 会限制请求并显示错误消息“远程服务器返回错误:(429)”。

如何批量执行CSOM PowerShell脚本?

假设,您想在列表中创建 1000 个项目。批量发送 100 个操作的 10 个请求而不是 1000 个不是更好吗?嗯,这个 CSOM PowerShell 脚本按给定的批次执行,并向服务器发送较少的请求。您可以根据您执行的操作调整批量大小参数。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/dev"
$ListName="Test"

#Batch Parameters
$BatchMax = 1000 #Total number of Operations to Perform
$BatchSize = 100 #Number of Operations for Each Batch
$BatchCounter = 1

Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        
    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #Batch Process the Creation of New items in the list
    While($BatchCounter -le $BatchMax)
    {
        #create list item in sharepoint online using powershell
        $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
        $ListItem = $List.AddItem($ListItemInfo)
        $ListItem["Title"] = $BatchCounter
        $ListItem.Update()
        
        #Perform the ExecuteQuery when the queue reaches the batch size
        If($BatchCounter % $BatchSize -eq 0 -or $BatchCounter -eq $BatchMax)
        {
            $Ctx.ExecuteQuery()
            Write-host "Executed Operations $BatchCounter of $BatchMax"
        }
        $BatchCounter++
    }
}
Catch {
    write-host -f Red "Error Adding Items to List!" $_.Exception.Message
}

当 SharePoint Online 在短时间内看到大量请求时,任何进一步的请求都将受到限制,并且您将看到错误“远程服务器返回错误:(429)”,HTTP 状态代码为 429(“请求过多”) ”)或 503(“服务器太忙”),请求将失败。虽然减少请求数量可以避免限制问题,但在请求之间添加“暂停”也有帮助。例如。,


#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Random"
$ItemsToCreate="10000"
$PauseSeconds = 5

Try {
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
         
    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Add items to list SharePoint Online
    For($i=1; $i -le $ItemsToCreate; $i++)
    {
        #create list item in sharepoint online using powershell
        $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
        $ListItem = $List.AddItem($ListItemInfo)
        $ListItem["Title"] = $I
        $ListItem.Update()

        Try{
            $Ctx.ExecuteQuery()
            Write-host "New Item $I Added to the List!" -ForegroundColor Green  
        }
        Catch [system.exception]{
            Start-Sleep -Seconds $PauseSeconds
            write-host -f Red "Error:" $_.Exception.Message
        }
    }
}
Catch {
    write-host -f Red "Error Adding Items to List!" $_.Exception.Message
}

Microsoft 的参考:避免在 SharePoint Online 中受到限制或阻止

PnP PowerShell 脚本具有类似的批处理概念,这在另一篇文章中进行了解释:如何在 PnP PowerShell 中更快地执行批量操作?但是,并非所有 cmdlet 都支持批处理!因此,您也可以将此技术与 PnP PowerShell 结合使用。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯