Created
July 28, 2015 15:50
-
-
Save Kevin-Bronsdijk/754e70d1e9bebb11adc9 to your computer and use it in GitHub Desktop.
sql-server-emergency-maintenance
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
Import-Module SQLPS -DisableNameChecking | |
#replace this with your instance name | |
$instanceName = "server\instance"; | |
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName; | |
$dbName = "DatabaseName" | |
$db = $srv.Databases.Item($dbName) | |
# Stops the all processes on the specified database. | |
$server.KillAllprocesses($dbName); | |
# Alter the files in SQL server (new path and/or name) | |
$db.FileGroups["NameFileGroup"].Files['LogicalName'].FileName = 'path\filename' | |
# Cannot move data files while online | |
$db.SetOffline(); | |
$db.Alter(); | |
# Move | |
Move-Item 'path\filename' 'path' | |
# And back online again | |
$db.SetOnline(); | |
$db.Alter(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment