Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creates a specified number of copies of a Database. Defaulted to clone the Tridion Content Manager Database 5 times
Param(
[Parameter(Mandatory=$False,Position=1)]
[string]$DatabaseName="Tridion_cm",
[Parameter(Mandatory=$False,Position=2)]
[string]$MachineName = $env:COMPUTERNAME,
[Parameter(Mandatory=$False,Position=3)]
[string]$InstanceName = "Default",
[Parameter(Mandatory=$False,Position=3)]
[int]$DataBaseCopies = 5
)
Set-Location SQLSERVER:\SQL\$MachineName\$InstanceName
#Global Variables
$DateTime = Get-Date -Format yyyyMMddHHmmss
#Get Server and Backup Path
$Server = Get-Item .
$backupPath = $Server.BackupDirectory
#Get Database
$Database = Get-SqlDatabase -Name $DatabaseName
$defaultDataPath = $Database.PrimaryFilePath
#Backup Database
$BackupFileName = $backupPath + "\" + $DatabaseName + "_$DateTime.bak"
Backup-SqlDatabase -Database $DatabaseName -BackupFile $BackupFileName
#Create Database Copies
for($i=1; $i -le $DataBaseCopies; $i++)
{
$copyDatabaseName = "$defaultDataPath\$DatabaseName"+"_$i.mdf"
$copyLogName = "$defaultDataPath\$DatabaseName"+"_log_$i.ldf"
$databaseLogicalName = $DatabaseName
$logLogicalName = $DatabaseName + "_log"
#Setup Relocate Files
$MoveFiles = @()
$MoveFiles += New-Object Microsoft.SqlServer.Management.Smo.RelocateFile ("$databaseLogicalName",$copyDatabaseName)
$MoveFiles += New-Object Microsoft.SqlServer.Management.Smo.RelocateFile ("$logLogicalName",$copyLogName)
$copyDatabaseName = $DatabaseName+"_$i"
Restore-SqlDatabase -Database $copyDatabaseName `
-RelocateFile $MoveFiles `
-BackupFile $BackupFileName `
-RestoreAction Database `
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment