AM trying to upload a bunch of files into SharePoint using PowerShell. I have a code that works on my local machine, but when I get on the server, it does not. Here is what my block of code looks like
$webUrl = "http://SampleSite"
$libraryName = "My Lib"
$docLibraryUrlName = "My%20ContentType"
$fileLocation = "C:\test\"
$contentType = "My ContentType"
#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$libraryName]
$files = ([System.IO.DirectoryInfo] (Get-Item $fileLocation)).GetFiles()
ForEach($file in $files)
{
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
# Gather the file name
$FileName = $File.Name
#remove file extension
$NewName = [IO.Path]::GetFileNameWithoutExtension($FileName)
#split the file name by the "-" character
$FileNameArray = $NewName.split("_")
#Add file
#$folder = $web.getfolder($docLibraryUrlName)
$folder = $web.getfolder($docLibrary)
write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
$spFile = $folder.Files.Add($folder.Url + "/" + $File.Name, $fileStream, $true)
$spItem = $spFile.Item
#populate metadata
$spItem["First Column"] = $FileNameArray[0]
$spItem["Second Column"] = $FileNameArray[1]
$spItem.Update()
$fileStream.Close();
}Again, this code works fine on my local machine but doesn't when I move this to the server. When I step through the code, I noticed that when I look at the data returned for my $folder variable in this snippet
$folder = $web.getfolder($docLibrary)
It shows the EffectiveRawPermissions to be "Open, BrowseUserInfo, UserClientIntegration" on the server...however, the EffectiveRawPermissions on my local machine is "FullMask". Does this have any effect on the ability of my code to be able to upload the files into SP on the server? I have never run into this issue, so am not sure how this makes sense...so I really appreciate any insight. Thanks
I am getting this error when the code attempts to perform the "Add" function...
Exception calling "Add" with "3" argument(s): "<nativehr>0x80070005</nativehr><nativestack></nativestack>Access denied."At C:\PowerShellScripts\tester.ps1:70 char:3
+ $spFile = $folder.Files.Add($folder.Url + "/" + $File.Name, $fileStream, $true ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : UnauthorizedAccessException ...Please help