Created
March 22, 2017 13:39
-
-
Save Krusen/94131f03946d3dac41b5edacbcf7a128 to your computer and use it in GitHub Desktop.
Attach/dettach databases with powershell
This file contains hidden or 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
# Run as Administrator | |
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } | |
Set-Location $PSScriptRoot | |
# Load configuration XML file. | |
[xml]$config = Get-Content "DatabasesConfig.xml" | |
#Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" | |
#Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" | |
[Reflection.Assembly]::LoadWithPartialName( "Microsoft.SqlServer.Smo" ) > $null | |
$smo = New-Object Microsoft.SqlServer.Management.Smo.Server $config.SQL.Server | |
try { | |
$smo.ConnectionContext.Connect() | |
} catch { | |
try { | |
$smo.ConnectionContext.LoginSecure = false | |
$smo.ConnectionContext.Login = $config.SQL.Credentials.Login | |
$smo.ConnectionContext.Password = $config.SQL.Credentials.Password | |
$smo.ConnectionContext.Connect() | |
} catch { | |
Write-Host "Can't connect to $($config.SQL.Server) as $(whoami)" -ForegroundColor Red | |
Write-Host "Can't connect to $($config.SQL.Server) with $($config.SQL.Credentials.Login):$($config.SQL.Credentials.Password)" -ForegroundColor Red | |
if ($Host.Name -eq "ConsoleHost") | |
{ | |
Write-Host "Press any key to exit..." | |
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null | |
} | |
return | |
} | |
} | |
ForEach ($database in $config.SQL.Databases.Database) | |
{ | |
$mdfFilename = $database.MDF | Resolve-Path | |
$ldfFilename = $database.LDF | Resolve-Path | |
$DBName = $database.DB_Name | |
$files = New-Object System.Collections.Specialized.StringCollection | |
$files.Add($mdfFilename) | Out-Null | |
$files.Add($ldfFilename) | Out-Null | |
try | |
{ | |
Write-Host "Attaching $DBName... " -NoNewline | |
$smo.AttachDatabase($DBName, $files, 'sa') | |
Write-Host "DONE" -ForegroundColor Green | |
} | |
catch [Exception] | |
{ | |
Write-Host "FAILED" -ForegroundColor Red | |
echo $_.Exception|format-list -force | |
} | |
} | |
# If running in the console, wait for input before closing. | |
if ($Host.Name -eq "ConsoleHost") | |
{ | |
Write-Host "Press any key to continue..." | |
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<SQL> | |
<Server>localhost\MSSQLSERVER2014</Server> | |
<Credentials> | |
<Login>sa</Login> | |
<Password>password</Password> | |
</Credentials> | |
<Databases> | |
<Database> | |
<MDF>Database.mdf</MDF> | |
<LDF>Database.ldf</LDF> | |
<DB_Name>DatabaseName</DB_Name> | |
</Database> | |
</Databases> | |
</SQL> |
This file contains hidden or 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
# Run as Administrator | |
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } | |
Set-Location $PSScriptRoot | |
# Load configuration XML file. | |
[xml]$config = Get-Content "DatabasesConfig.xml" | |
#Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" | |
#Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" | |
[Reflection.Assembly]::LoadWithPartialName( "Microsoft.SqlServer.Smo" ) > $null | |
$smo = New-Object Microsoft.SqlServer.Management.Smo.Server $config.SQL.Server | |
try { | |
$smo.ConnectionContext.Connect() | |
} catch { | |
try { | |
$smo.ConnectionContext.LoginSecure = false | |
$smo.ConnectionContext.Login = $config.SQL.Credentials.Login | |
$smo.ConnectionContext.Password = $config.SQL.Credentials.Password | |
$smo.ConnectionContext.Connect() | |
} catch { | |
Write-Host "Can't connect to $($config.SQL.Server) as $(whoami)" -ForegroundColor Red | |
Write-Host "Can't connect to $($config.SQL.Server) with $($config.SQL.Credentials.Login):$($config.SQL.Credentials.Password)" -ForegroundColor Red | |
if ($Host.Name -eq "ConsoleHost") | |
{ | |
Write-Host "Press any key to exit..." | |
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null | |
} | |
return | |
} | |
} | |
ForEach ($database in $config.SQL.Databases.Database) | |
{ | |
$DBName = $database.DB_Name | |
try | |
{ | |
Write-Host "Detaching $DBName... " -NoNewline | |
$smo.KillAllProcesses($DBName) | |
$smo.DetachDatabase($DBName, $false) | |
Write-Host "DONE" -ForegroundColor Green | |
} | |
catch [Exception] | |
{ | |
Write-Host "FAILED" -ForegroundColor Red | |
} | |
} | |
# If running in the console, wait for input before closing. | |
if ($Host.Name -eq "ConsoleHost") | |
{ | |
Write-Host "Press any key to continue..." | |
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment