Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."

Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."

如果我 运行 PowerShell 脚本 below:Exception 使用“0”参数调用 "ExecuteQuery",我会收到以下错误:"The remote server returned an error: (401) Unauthorized."

我在本网站和其他网站上阅读了一些 post,但它们都超过 3 年了,无法解决我的问题。

该脚本运行良好,但从上周二开始,powershell 脚本出现此异常。没有改变。 我尝试 运行 不同计算机上的脚本,但结果相同。 如果我使用其他用户,错误仍然存​​在。 SharePoint 没有报告问题,我可以通过浏览器登录到 SharePoint。

希望你能帮助我。感谢您的帮助!

    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"

    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force

    #Bind to site collection
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SharePointUri)
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, $SecurePassword)
    $Context.Credentials = $Credentials

    #Retrieve list
    $List = $Context.Web.Lists.GetByTitle($DocumentLibrary)
    $Context.Load($List.RootFolder)
    $Context.ExecuteQuery();```

问题已通过 SharePoint PNP 模块解决。 https://github.com/pnp/PnP-PowerShell