Skip to content

Instantly share code, notes, and snippets.

@michaellwest
Last active March 26, 2018 16:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaellwest/70a1588edbef7993dacf07eff7e88638 to your computer and use it in GitHub Desktop.
Save michaellwest/70a1588edbef7993dacf07eff7e88638 to your computer and use it in GitHub Desktop.
Bulk Data Generator provides a UI to generating sample data. Tested with SPE 4.3 on Sitecore 8.1.
<#
.SYNOPSIS
Bulk generates items or users using the specified criteria.
#>
$dataTypeOptions = [ordered]@{"Item" = 1; "User" = 2 }
$props = @{
Title = "Bulk Creation Tool"
Description = "Choose what type of data to generate."
OkButtonName = "Next"
CancelButtonName = "Quit"
Parameters = @(
@{Name="Info"; Title="Warning"; Value="This tool is not intended for use on production systems."; editor="info";},
@{Name="selectedDataTypeValue"; Value=1; Options=$dataTypeOptions; Title="What type of data should be generated?"}
)
}
$result = Read-Variable @props
if($result -ne "ok") {
exit
}
$dataTypeName = $dataTypeOptions.Keys | Where-Object { $dataTypeOptions[$_] -eq $selectedDataTypeValue } | Select-Object -First 1
$defaultCount = 10
$defaultItemNamePrefix = "Sample"
$contextItem = Get-Item -Path .
$database = $contextItem.Database.Name
$defaultRootItem = Get-Item -Path (@{$true="$($database):\content\home"; $false="$($database):\content"}[(Test-Path -Path "$($database):\content\home")])
if($contextItem.Paths.Path.StartsWith($defaultRootItem.Paths.Path)) {
$defaultRootItem = $contextItem
}
$defaultDomain = "sitecore"
$domainList = (Get-Domain | Select-Object -Expand Name | ForEach-Object { $_ + "|" + $_ }) -join "|"
$defaultItemTemplate = Get-Item "master:\templates\Sample\Sample Item"
$props = @{
Title = "Bulk $($dataTypeName) Creation Tool"
Description = "Configure how the $($dataTypeName)s should be generated."
OkButtonName = "Generate"
CancelButtonName = "Abort"
Width = 550
Height = 450
Parameters = @(
@{
Name="inputCount"; Value=$defaultCount; Placeholder="Enter the number of items";
Title="How many $($dataTypeName)s should be generated?"; Columns=6;
Validator = {
if([string]::IsNullOrEmpty($variable.Value) -or -not($variable.Value -is [int])) {
$variable.Error = "The item count should be between 1 and 100."
}
}
}
)
}
if($dataTypeName -eq "Item") {
$props.Parameters += @{
Name="itemNamePrefix"; Value=$defaultItemNamePrefix; Placeholder="Enter item name prefix";
Title="What should be the $($dataTypeName) name prefix?"; Columns=6;
Validator = {
if([string]::IsNullOrEmpty($variable.Value) -or [Sitecore.Data.Items.ItemUtil]::IsItemNameValid($variable.Value)) {
$variable.Error = "The item name prefix is invalid."
}
}
}
$props.Parameters += @{
Name="selectedrootItem"; Value=$defaultRootItem;
Title = "Where should the Items be generated?";
root="/sitecore/content/"; Editor="Droptree";
Validator = {
if([string]::IsNullOrEmpty($variable.Value)) {
$variable.Error = "The item root is missing."
}
}
}
$props.Parameters += @{
Name="selectedItemTemplate"; Value=$defaultItemTemplate;
Title = "Which Data Template should be used?";
root="/sitecore/templates/"; Editor="Droptree";
Validator = {
if([string]::IsNullOrEmpty($variable.Value)) {
$variable.Error = "The item template is missing."
}
}
}
} elseif($dataTypeName -eq "User") {
$props.Parameters += @{Name="defaultPassword"; Value=""; Title = "Default Password"; Columns=6; Placeholder="Password (optional)"; }
$props.Parameters += @{Name="selectedDomain"; Value=$defaultDomain; Options=$domainList; Title = "Which domain should be used when generating Users?"; Columns=9; }
$props.Parameters += @{Name="enableUsers"; Value=$false; Title = "Enable Users"; Columns=3; }
}
$result = Read-Variable @props
if($result -ne "ok") {
exit
}
if($dataTypeName -eq "Item") {
$rootItemPath = $selectedrootItem.FullPath
$childCount = $selectedrootItem.Children.Count + 1
for($index = $childCount; $index -lt $childCount + $inputCount; $index++) {
New-Item -Path $rootItemPath -Name "$($itemNamePrefix)$($index)" -Type $selectedItemTemplate.Paths.Path
}
} elseif($dataTypeName -eq "User") {
$firstNames = "Michael Rebecca Adam Jason Martha Alan David Khris Robert Chris Steven Peter Matthew Mark Luke John Thomas Sarah Lisa Martha" -split " "
$lastNames = "West Smith Johnson Williams Brown Jones Martinez Thompson White Wilson Harrison Lee Beckham" -split " "
for($index = 0; $index -lt $inputCount; $index++) {
$first = $firstNames[(Get-Random -Maximum $firstNames.Count)]
$middle = [char](65 + (Get-Random -Maximum 26))
$last = $lastNames[(Get-Random -Maximum $lastNames.Count)]
$randomId = Get-Random -Maximum 9999 -Minimum 1000
$userId = "$($first[0])$($middle)$($last)$($randomId)".ToLower()
$commandProperties = @{
"Identity" = "$($selectedDomain)\$($userId)"
"FullName" = "$($first) $($middle) $($last)"
"Email" = "$($userId)@testdomain.com"
}
if($enableUsers) {
$commandProperties["Enabled"] = $enableUsers
$commandProperties["Password"] = @{$true=[System.Web.Security.Membership]::GeneratePassword(10,3);$false=$defaultPassword}[[string]::IsNullOrEmpty($defaultPassword)]
}
New-User @commandProperties
}
}
@michaellwest
Copy link
Author

michaellwest commented Jan 16, 2017

Feedback:

  • Check for existing username
  • Add "real name" to the user
  • Add option to enable/disable users
  • Add option to specify default password
  • Check for context menu selected item before defaulting to the home item
  • Add validation check for the prefix
  • Add validation for the template name
  • Add validation for the root path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment