none
CSOM PowerShell not working without admin center access RRS feed

  • Question

  • I've written a CSOM PowerShell script to copy files from document library in SharePoint Online site to Shared drive. It seems working for SharePoint Online site from 1 month free Office 365 subscription where I do have admin center access. But it is not working for my project SharePoint online site where I do have site collection admin access but not admin center access. As it is CSOM PowerShell, it should work without admin center access. Please see the script below and suggest if admin center access can be bypassed.

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
    [System.Reflection.Assembly]::LoadWithPartialName("System.IO.MemoryStream")
    $SourceSiteURL = "*******"

    $UserName = "****"
    $Password = ConvertTo-SecureString "*" -AsPlainText -Force
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)
    $SourceLibrary = "***"


    $SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
    $SourceCtx.Credentials = $Credentials
    if(!$SourceCtx.ServerObjectIsNull.value){ Write-Host "Connected to Site" -ForegroundColor Green }
    Function DownloadFilesFromFolder()
    {

           $diskLocation = "\\**\*\*";

           $docLib = $SourceCtx.Web.Lists.GetByTitle($SourceLibrary);
           $SourceCtx.Load($docLib);
           $SourceCtx.ExecuteQuery();

           $listRootFolder = $docLib.RootFolder;
           $FilesInRootFolder = $listRootFolder.Files;
           $SourceCtx.Load($FilesInRootFolder);
           $SourceCtx.ExecuteQuery();

           foreach($file in $FilesInRootFolder)
           {
                  Write-Host $file.Name
                  if($SourceCtx.HasPendingRequest)
                  {
                          $SourceCtx.ExecuteQuery();
                  }
                  $fileRef = $file.ServerRelativeUrl;
                  $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceCtx, $fileRef);
                  $fileName = $diskLocation + $file.Name;
                  $fileStream = [System.IO.File]::Create($fileName)
                  $fileInfo.Stream.CopyTo($fileStream)
                  $fileStream.Close();
           }
    }
    DownloadFilesFromFolder

    Thanks,

    Kunal


    Kunal

    Wednesday, September 25, 2019 7:11 AM