Last active
January 29, 2016 02:02
-
-
Save PCfromDCSnippets/86b6609693d411300ba8 to your computer and use it in GitHub Desktop.
backupDatabases_Orig
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
$bkdir = "\\SQL2012B\Shared\Temp" # Set Backup Path! | |
# SSMS needed to be installed to load the SQL Assemblies | |
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null | |
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | out-null | |
$s = new-object ("Microsoft.SqlServer.Management.Smo.Server") $instance | |
# Grabs ALL of the databases | |
$dbs = $s.Databases | |
foreach ($db in $dbs) | |
{ | |
if(($db.Name -ne "tempdb") -and ($db.Name -ne "master") -and ($db.Name -ne "model") -and ($db.Name -ne "msdb")) | |
{ | |
$dbname = $db.Name | |
$dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup") | |
$dbBackup.Action = "Database" | |
$dbBackup.Database = $dbname | |
$dbBackup.Devices.AddDevice($bkdir + "\" + $dbname + ".bak", "File") | |
$dbBackup.SqlBackup($s) | |
write-host($db.name + " has been backed up.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment