Created
January 29, 2016 02:23
-
-
Save PCfromDCSnippets/dcfeabb856e1cc576250 to your computer and use it in GitHub Desktop.
Restore SQL Databases
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
$restoreDir = "J:\Backups" | |
# Get files in backup directory | |
$files = get-childitem $restoreDir -recurse | |
foreach ($file in $files) | |
{ | |
$query = "RESTORE DATABASE [" + $file.basename + "] | |
FROM DISK = N'" + $restoreDir + $file + "' WITH FILE = 1, | |
MOVE N'" + $file.basename + "' TO N'" + $dataLocation + $file.basename + ".mdf', | |
MOVE N'" + $file.basename + "_log' TO N'" + $logLocation + $file.basename + "_log.LDF', | |
NOUNLOAD, | |
STATS = 5" | |
invoke-sqlcmd -query $query -Database "Master" -ServerInstance $serverInstance -Username "SA" -Password "Password1" | |
write-host ("$query") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment