Author Posts

October 25, 2017 at 4:18 am


I'm trying to upload files to sharepoint online using a powershell script. I get an error "access to the path ... was denied".

I can upload my file using Internet Explorer but I can't do it using my script.

The script is :

    $sourceDocumentPath = "C:\tests_onedrive\travail\ANGELA\SON\20151125_133740_resized-2.jpg" # Source document to spawn new documents from for the creation
    $User = ""

    $webUrl = ""
    $docLibraryName = "Documents"

    #Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    #Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
    $Password = Read-Host -Prompt "Please enter your password" -AsSecureString

    # ————————- #Open web and library

    #Bind to site collection
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
    $Context.Credentials = $Creds

    #Retrieve list
    $uploadfolder = $Context.Web.Lists.GetByTitle($docLibraryName)

    $File = get-item $sourceDocumentPath
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)

    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File.Name
    #$FileCreationInfo.URL = $File.Name

    write-host $FileCreationInfo.URL

    $Upload = $uploadfolder.RootFolder.Folders.GetByUrl("Folder").Files.Add($FileCreationInfo)



The error message :

    New-Object : Exception calling ".ctor" with "2" argument(s): ""Access to the path 'C:\tests_onedrive\travail\ANGELA\SON\20151125_133740_resized-2.jpg' was denied."
    At C:\Users\Administrateur\Desktop\tests_onedrive\Script\ESSAI.ps1:55 char:15
    + ... ileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMo ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Can you help me ?

Thanks, Regards,

October 25, 2017 at 12:12 pm

"Access to the path 'C:\tests_onedrive\travail\ANGELA\SON\20151125_133740_resized-2.jpg' was denied."

Your permissions error seems to be coming from your local drive. Are you using different credentials to run the script than you are using when you upload via IE? Are you running the script from an elevated shell or standard shell?