Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerTip: Use PowerShell to Back Up System Databases

$
0
0

Summary: Use Windows PowerShell to back up system databases.

Hey, Scripting Guy! Question How can I use Windows PowerShell to dynamically back up all of the system databases on my servers?

Hey, Scripting Guy! Answer Combining provider lookups with the Backup-SqlDatabase cmdlet makes this simple:

$instances = @(‘KIRK’,’SPOCK’,’PICARD’,’RIKER’)

foreach($instance in $instances){

            $dbs = Get-ChildItem SQLSERVER:\SQL\$instance\DEFAULT\Databases -Force |

Where-Object {$_.IsSystemObject -eq $true -and $_.Name -ne 'TempDB'}

    $dbs |

ForEach-Object {

Backup-SqlDatabase -ServerInstance $instance -Database $_.Name -BackupFile "C:\DBFiles\$($_.Name).bak" -Initialize }}

$instances |

ForEach-Object {Invoke-Command -ComputerName $_ -ScriptBlock {Get-ChildItem C:\DBFiles\*.bak}}


Viewing all articles
Browse latest Browse all 3333

Trending Articles