Function Upload-FilesFromServer()
{
param
(
[Parameter(Mandatory=$true)] [ClientContext] $Ctx,
[Parameter(Mandatory=$true)] [string] $SourceFiles,
[Parameter(Mandatory=$true)] [string] $TargetDocLib
)
#Retrieve list
$List = $Ctx.Web.Lists.GetByTitle($TargetDocLib)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
$TargetFolder = $Ctx.Web.GetFolderByServerRelativeUrl("Documents" + "/" + "Numbers" + "/" + "SubNumbers")
#Upload file
Foreach ($File in (Dir $SourceFiles -File))
{
$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
$Upload = $TargetFolder.Files.Add($FileCreationInfo)
$Ctx.Load($Upload)
$Ctx.ExecuteQuery()
}
}
When I run the code I get:
Exception calling "ExecuteQuery" with "0" argument(s): "File Not Found."
At line:27 char:9
+ $Ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ServerException
When I checked the variable $FileCreationInfo, it has all the file information.
Sometimes I would get this running the same code above:
Exception calling "ExecuteQuery" with "0" argument(s): "Access denied. You do not have
permission to perform this action or access this resource."
At line:28 char:9
+ $Ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ServerUnauthorizedAccessException
But I have global admin access.
I'm trying to upload files to a folder that's several folders down such as: Documents/Numbers/SubNumbers.