使用 Powershell 将文件上传到 Sharepoint Online (Microsoft 365)(选项 1-使用 PnP.Powershell)

Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 1-Using PnP.Powershell)

我正在尝试将文件上传到 Sharepoint Online (M365) 库子文件夹,但它一直给我错误。我试过很多脚本。 post 是关于使用 PnP.Powershell(我对其他脚本有 post 的问题,希望有人能帮助我)

这是代码:

$url="https://mydomain.sharepoint.com/sites/mysharepointsite/" 

$userID = "mail@foo.com"
$securePassword = ConvertTo-SecureString -String "myPaswword" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $userID, $securePassword

Install-Module -Name PnP.PowerShell
Connect-PnPOnline $url -Credentials $credentials

$files = Get-Item -Path "C:\myFolder\myFile.csv" -Force
foreach ($file in $files)
{
    Add-PnPFile -Folder "myLibraryname/subfolder"  -Path $file.FullName
    Write-Host $done.Name  "Uploaded into the Site" -BackgroundColor Green
}

它给我这个错误

Connect-PnPOnline:AADSTS65001:用户或管理员未同意使用 ID 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' 名为 'PnP Management Shell' 的应用程序。为此用户和资源发送交互式授权请求。

如何在 M365 中授予此权限?授予它安全吗?

谢谢

需要为您的租户授予 PnP PowerShell 的一些权限。

也许你可以试试

Register-PnPManagementShellAccess

参考:Connecting with PnP PowerShell

我想如果你确保你使用的是官方版本而不是自定义(黑客)版本,那是安全的。

基本上,它将使用委托授权授予模块执行某些任务的权利。它的权限不会超过您的帐户。