I needed to make some tests on Sharepoint Tresholds, so I needed a small script that uploads a large amount of documents (The same document) in my Doc Libraries.
For that I just created in the same folder where my Powershell script is running a small Test.txt file. And Uploaded it a given number of times under a new name.
Here's the script:
if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
clear
$uploadURL = "http://MYWEBURL/"
$fileName = "test.txt"
$uploadLibraries = @("Documents", "Documents2")
#I'm Uploading to the root, but you could put there a folder like "Documents/FolderName"
#I want to uplaod 6000 docs on each library... that takes a while
$TOTALDOCSTOUPLOAD =6000
$startFileIndex = 0
$thisScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$oldFilePath = $thisScriptPath + "\" + $fileName
$site = Get-SPSite -Identity $uploadURL
if($site -ne $null){
$web = $site.OpenWeb()
if($web -ne $null -and $web.Exists){
foreach($uploadLibrary in $uploadLibraries)
{
try
{
$docLib = $web.GetFolder("/"+$uploadLibrary)
}
catch
{
write-host 'Document Library is unavailable'
$docLib = $null
}
for($i = $startFileIndex; $i -lt $TOTALDOCSTOUPLOAD; $i++)
{
if($docLib -ne $null)
{
$fileParts = $fileName.split(".")
$fileParts[0] = $fileParts[0]+ $i.ToString()
$newFileName = [string]::join('.', $fileParts)
$newFilePath = $thisScriptPath + "\" + $newFileName
Rename-Item $oldFilePath $newFilePath
$docUrl = $docLib.ServerRelativeUrl + "/" + $newFileName
if($docLib.Files[$docurl].Exists)
{
$file = $docLib.Files[$docurl]
if($file.LockType -eq "None" -and $docLib.RequiresCheckout)
{
$file.CheckOut()
}
}
$fileStream =$((Get-ChildItem $newFilePath).OpenRead())
$docLib.Files.Add($docUrl, $fileStream, $true) | Out-Null
$file = $docLib.Files[$docurl]
if($file.CheckOutType -ne "None")
{
if($docLib.RequiresCheckout) {
$file.CheckIn("Updated Template", 1)
}
}
$fileStream.Dispose()
Rename-Item $newFilePath $oldFilePath
write-Host "Uploade "$newFilePath "to "$uploadLibrary
}
}
}
$web.Update()
$web.Dispose()
}
$site.Dispose()
}
No comments:
Post a Comment