Created
April 11, 2019 19:47
-
-
Save asvignesh/c22cae2abae7d5d5b01d11435e98e086 to your computer and use it in GitHub Desktop.
Create / Restore Database from Adventure work backup file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import-module sqlps | |
$total =2 | |
$volume1 = "E:" | |
$volume2 = "F:" | |
$backupLocation = "C:\Users\asvignesh\Downloads\AdventureWorks2016.bak" | |
$database_prefix = "vignesh-testing" | |
foreach($count in 1..$total) | |
{ | |
$database = $database_prefix +"-" +$count | |
$dataFileLocation = $volume1+"\"+$database+"\" | |
$logFileLocation = $volume2+"\"+$database+"\" | |
foreach($path in $dataFileLocation, $logFileLocation){ | |
if(!(Test-Path -Path $path) ){ | |
New-Item -ItemType directory -Path $path | |
Write-Host "New folder created" | |
} | |
} | |
$sql = @" | |
USE [master] | |
RESTORE DATABASE [$database] | |
FROM DISK = N'$backupLocation' | |
WITH FILE = 1, | |
MOVE N'AdventureWorks2016_Data' TO N'$dataFileLocation$database.mdf', | |
MOVE N'AdventureWorks2016_Log' TO N'$logFileLocation$database.ldf', | |
NOUNLOAD, REPLACE, STATS = 5 | |
ALTER DATABASE [$database] | |
SET MULTI_USER | |
"@ | |
invoke-sqlcmd $sql -ServerInstance NETWORK1\CLUSTER1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment