简体   繁体   中英

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

I'm trying to upload a file into a Sharepoint Online (M365) library subfolder, but it keeps giving me errors. I have tried many scripts. This post is about using PnP.Powershell (I have posted questions about other scripts hoping someone can help me with any of them)

This is the code:

$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
}

It gives me this error

Connect-PnPOnline : AADSTS65001: The user or administrator has not consented to use the application with ID 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' named 'PnP Management Shell'. Send an interactive authorization request for this user and resource.

How do I grant this permission in M365? Is it safe to grant it?

Thanks

PnP PowerShell need to be granted some privileges on your tenant.

Maybe you can try

Register-PnPManagementShellAccess

Ref: Connecting with PnP PowerShell

I guess it's safe if you ensure you are on the official build and not a customized (hacked) one.

Basically, it will grant the module the right to perform some task using a delegated authorization. It won't have more privileges than your account.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM