-1

Please teach me. I am a beginner. I want to upload all the data of the local folder to the "General" folder of Sharepoint online(Folder A/files, Folder A/Folder B/Folder C/files etc.).

This code is Four arguments error.

$SubFolder = (($file.Directoryname).Replace($localFolder,'')).Replace('\','/')
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($script:context, $script:context.Url + "/" + $rootfolder.Name + "/" + $folderName +  $SubFolder + "/" + $file.Name, $fs, $true)

$SubFolder is /A etc.

Olaf
  • 4,690
  • 2
  • 15
  • 23

1 Answers1

0

You may try below script here, guide here.

function UploadDocuments(){
Param(
        [ValidateScript({If(Test-Path $_){$true}else{Throw "Invalid path given: $_"}})] 
        $LocalFolderLocation,
        [String] 
        $siteUrl,
        [String]
        $documentLibraryName
)
Process{
        $path = $LocalFolderLocation.TrimEnd('\')

        Write-Host "Provided Site :"$siteUrl -ForegroundColor Green
        Write-Host "Provided Path :"$path -ForegroundColor Green
        Write-Host "Provided Document Library name :"$documentLibraryName -ForegroundColor Green

          try{
                $credentials = Get-Credential

                Connect-PnPOnline -Url $siteUrl -CreateDrive -Credentials $credentials

                $file = Get-ChildItem -Path $LocalFolderLocation -Recurse
                $i = 0;
                Write-Host "Uploading documents to Site.." -ForegroundColor Cyan
                (dir $path -Recurse) | %{
                    try{
                        $i++
                        if($_.GetType().Name -eq "FileInfo"){
                          $SPFolderName =  $documentLibraryName + $_.DirectoryName.Substring($path.Length);
                          $status = "Uploading Files :'" + $_.Name + "' to Location :" + $SPFolderName
                          Write-Progress -activity "Uploading Documents.." -status $status -PercentComplete (($i / $file.length)  * 100)
                          $te = Add-PnPFile -Path $_.FullName -Folder $SPFolderName
                         }          
                        }
                    catch{
                    }
                 }
            }
            catch{
             Write-Host $_.Exception.Message -ForegroundColor Red
            }

  }
}


UploadDocuments -LocalFolderLocation C:\Lee\Share -siteUrl https://domain.sharepoint.com/sites/Developer -documentLibraryName MyDOc4
Lee
  • 5,305
  • 1
  • 6
  • 12