-1

Using the SharePointPnPPowerShellOnline module, to upload a file, to a specific folder (in a specific site), I used to run commands like this:

$cred = Get-Credential
Connect-PnPOnline -Url "https://<company>.sharepoint.com/sites/<site name>" -Credentials $cred -ErrorAction Stop
$memStream = [System.IO.MemoryStream]([System.Text.Encoding]::UTF8.GetBytes(($data | ConvertTo-Csv -NoTypeInformation)))
Add-PnPFile -FileName $fileName -Folder "/Shared Documents/<folder>/<sub folder>" -Stream $memStream -ErrorAction Stop

Since SharePointPnPPowerShellOnline is being replaced by PnP.PowerShell, I tried running the same command in with the new module installed. After adding the required consent (Register-PnPManagementShellAccess), I got the error:

Connect-PnPOnline : The remote server returned an error: (403) Forbidden.

I changed the connect command to:

Connect-PnPOnline -Url "https://<company>.sharepoint.com/" -Credentials $cred -ErrorAction Stop

That seemed to work:

VERBOSE: Acquiring token

VERBOSE: Token acquired VERBOSE: PnP

PowerShell Cmdlets (1.10.0.0)

When I ran the Add-PnPFile command again, there were no errors, but I cannot find the file in SharePoint. What am I missing here?

StackExchangeGuy
  • 741
  • 16
  • 36

1 Answers1

0

Do you have multi-factor authentication enabled on the account? If yes, please try to use -UseWebLogin switch parameter ,the connect command:

Connect-PnpOnline -Url https://xxxx.sharepoint.com/sites/xxx -UseWebLogin 

My test script and results(upload a file to a specific folder):

Connect-PnpOnline -Url https://xxxxx.sharepoint.com/sites/xxxx -UseWebLogin 
$localpath = "C:\Users\Administrator\Desktop\upload.txt"  
$splib = "test_library\test"  
Add-PnPFile -Path $localPath -Folder $splib 

enter image description here

enter image description here

Zella_msft
  • 372
  • 2
  • 4