Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

SP 2016 PowerShell - Upload Files Script

$
0
0

Hello Community,

I have the following script which is designed to upload files to SharePoint in bulk, I've worked through one problem already with the help of Jerry Zy (thanks again jerry!) now I'm on to the next issue whenever I run the script, I get the error below for every file (yet weird thing is it ran once for one set of files without problem!).  Here is the error:

Copying file  CS-1068.docx  to  /sites/testuploadfiles/SSW_Test ...
Exception calling "Add" with "3" argument(s): "The URL 'SSW_Test/CS-1068.docx' is invalid.  It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web."
At D:\Scripts\ImportFilesFromDirtoSP.ps1:37 char:5+     $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [Syst ...+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException+ FullyQualifiedErrorId : SPException

And here is the full script:

cls

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{    Add-PSSnapin Microsoft.SharePoint.PowerShell
    Write-Host "SP Add-in Activated"
} 
#Script settings 
$webUrl = "https://mytools-tst16/sites/testuploadfiles/"  
$docLibraryName = "SSW_Test"
$docLibraryUrlName = "SSW_Test" 
$localFolderPath = "D:\DownloadFiles\SSWs" 
#Open web and library 
$web = Get-SPWeb $webUrl
Write-Host "webSite = " + $web.Url 
$docLibrary = $web.Lists[$docLibraryName]
Write-Host "docLibraryName = " + $docLibraryName 
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() 
ForEach($file in $files)
{     #Open file    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()     #Add file    $folder =  $web.getfolder($docLibraryUrlName)     write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)    write-host "Success"     #Close file stream    $fileStream.Close();
} 
#Dispose web 
$web.Dispose()

Has anyone ever seen this problem?  if you know how to fix it please provide guidance and examples.

Thanks!

Tom


Tom Molskow - Senior SharePoint Architect - Microsoft Community Contributor 2011 and 2012 Award - Linked-In - SharePoint Gypsy


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>