[Q&A] How to generate tons of test documents for SharePoint

qanda

Q: Is there any way to generate tons of test documents and upload them into random SharePoint document libraries?

A: Sure, you can use script below to generate documents with GUID as a title in order to guarantee every document will have a unique title. Document content will be selected from an array $words.

Note: Please make sure you have Office 2013 or 2016 installed otherwise script will not be executed.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$words = @("Cat", "Dog","Cow","Giraffe", "Ball", "Cube", "Sphere", "Document", "Paper", "Computer", "The", "Person", "Black", "Blue", "Red", "SharePoint", "Microsoft", "Red", "Search", "Create", "Delete", "Article", "Web", "HTML", "C#", "VB", "Microsoft", "Job", "Office", "Table", "Star", "Pen", "Pencil", "Arm", "Leg", "Doctor", "Student", "Education", "Defense", "Army", "Navy", "Boat", "Plane", "Jet", "Hockey", "Baseball", "Soccer", "Volley-Ball", "Swim", "Pool", "Lake", "Ocean", "Sea", "Bird", "Fly", "Sky", "Planet", "Space", "Pig", "Owl", "Night", "Day", "Morning", "Evening", "Delete", "Create", "Update", "Great", "Super", "Awesome", "Mark", "Check", "Report", "Minutes", "Decisions", "Technology","Software", "Hardware", "Server", "Database", "Application", "Game", "Keyboard", "Mouse", "Screen", "Tower", "Building", "Street", "Song", "Music", "Car", "Bus")

$number = Read-Host "How many documents do you want to randomly upload to SharePoint?"

$allWebApps = Get-SPWebApplication

[ref]$SaveFormat = "Microsoft.Office.Interop.Word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $false
for($i = 0; $i -lt $number; $i++)
{
    $filePath = "C:\DND\Documents\" + [guid]::NewGuid().ToString() + ".docx"

    $doc = $word.documents.add()

    $selection = $word.selection
    $result = $selection.WholeStory
    $selection.Style = "No Spacing"

    $rndNumberOfWords = Get-Random -Minimum 1 -Maximum 1000
    $sentence = ""
    for($j = 0; $j -le $rndNumberOfWords; $j++)
    {
        $sentence += $words[(Get-Random -Minimum 0 -Maximum ($words.Length))] + " "
    }

    $selection.font.size = 14
    $selection.font.bold = 1
    $selection.typeText($sentence)

    $result=$doc.saveas([ref] $filePath, [ref]$saveFormat::wdFormatDocument)
    $result = $doc.Close()

    $randomWebApp = $allWebApps[(Get-Random -Minimum 0 -Maximum $allWebApps.Length)]
    $randomSite = $randomWebApp.Sites[(Get-Random -Minimum 0 -Maximum ($randomWebApp.Sites.Count-1))]

    $url = $randomSite.Url
    Write-Host "Uploading a random document to"$url

    $spWeb = Get-SPWeb $url
    $docLib = $spWeb.GetFolder("Shared Documents")
    $allFiles = $docLib.Files 

    $fileName = $filePath.Substring($filePath.LastIndexOf("\")+1) 

    $fileObject = Get-ChildItem $filePath

    $stream = $fileObject.OpenRead()
    $result = $allFiles.Add("Shared Documents/" + $fileName, $stream, $false)
    $stream.Close()
    $fileObject = $null

    $spWeb.Dispose()
    Remove-Item $filePath -ErrorAction SilentlyContinue -Force
}
$word = $null
Advertisements

Добавить комментарий

Заполните поля или щелкните по значку, чтобы оставить свой комментарий:

Логотип WordPress.com

Для комментария используется ваша учётная запись WordPress.com. Выход / Изменить )

Фотография Twitter

Для комментария используется ваша учётная запись Twitter. Выход / Изменить )

Фотография Facebook

Для комментария используется ваша учётная запись Facebook. Выход / Изменить )

Google+ photo

Для комментария используется ваша учётная запись Google+. Выход / Изменить )

Connecting to %s