Powershell, SMO and Database Files

In response to some questions about renaming a physical file for a database, I have 2 versions of Powershell scripts that do this for you, including taking the database offline and then online to make the physical change match the meta-data.

First, there is an article about this at http://msdn.microsoft.com/en-us/library/ms345483.aspx.  This explains that you start by setting the database offline, then alter the database and modify the filename then set it back online.  This particular article does not show the physical renaming of the file, but it is necessary since the ALTER DATABASE MODIFY FILE statement only changes the metadata of the database in master.

There is a procedure to do this with SQL CLR and TSQL, but I chose to illustrate it in Powershell and SMO (using SQL Provider as well as straight SMO).  The SQLCLR version is by a SQL Server MVP, Ted Krueger (Blog | Twitter) and can be found at this link.

My version will be based on Powershell and SMO with a smattering of SQL Provider provided in SQL 2008+.

Code Snippet
  1. Add-PSSnapin SqlServerCmdletSnapin100
  2. Add-PSSnapin SqlServerProviderSnapin100
  3.  
  4. $servername = “localhost”
  5. $instance = “default”
  6. $dbname = “N2CMS”
  7. $logicalName = “N2CMS”
  8. $NewPath = “c:\program files\microsoft sql server\mssql10_50.mssqlserver\mssql\data”
  9. $NewFilename = “N2CMS_4.mdf”
  10.  
  11. if(Test-Path “sqlserver:\sql\$servername\$instance\databases\$dbname”)
  12. {
  13.         $database = Get-Item(“sqlserver:\sql\$servername\$instance\databases\$dbname”);
  14.  
  15.         $fileToRename = $database.FileGroups[“PRIMARY”].Files[$logicalName]
  16.         $InitialFilePath = $fileToRename.FileName
  17.  
  18.         $fileToRename.FileName = “$NewPath\$NewFilename”
  19.  
  20.         $database.Alter();
  21.         $database.SetOffline()
  22.         Rename-Item -Path $InitialFilePath -NewName “$NewFilename”
  23.         $database.SetOnline()
  24.         Write-Host “File Renamed”
  25. }
  26. else
  27. {
  28.         Write-Host “Database does not exist”;
  29. }

If you notice, I first add the Snapin that is the SQL Provider.  If you already have it loaded in your Powershell Profile, then you can just omit those lines.  If you do not have SQL 2008 objects installed on this machine, then you will notice that the Snapins don’t load either.  In that case you would just use the version below.

In the SQL Provider version you will see a Powershellism with Test-Path and you are using a path, it just is a SQL provider path that points to the Database and makes sure that it exists.  Compare it to the other version where you are looking at the Server.Databases[$dbname] and then you get the filegroups and then the files.

This version of the script is purely SMO and PowerShell.  First you load the objects from SQL Server 2005/2008 SMO.

Code Snippet
  1. # Always
  2. [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”) | outnull
  3.  
  4. # Only if you don’t have SQL 2008 SMO Objects installed
  5. [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SmoEnum”) | outnull
  6.  
  7. # Only if you have SQL 2008 SMO objects installed
  8. [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SqlEnum”) | outnull
  9.  
  10. $servername = “localhost”
  11. $instance = “default”
  12. $dbname = “N2CMS”
  13. $logicalName = “N2CMS”
  14. $NewPath = “c:\program files\microsoft sql server\mssql10_50.mssqlserver\mssql\data”
  15. $NewFilename = “N2CMS_4.mdf”
  16.  
  17. $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $servername
  18. if($server.Databases[$dbname] != $null)
  19. {
  20.         $database = $server.Databases[$dbname];
  21.         $fileToRename = $database.FileGroups[“PRIMARY”].Files[$logicalName]
  22.         $InitialFilePath = $fileToRename.FileName
  23.         $fileToRename.FileName = “$NewPath\$NewFilename”
  24.         $database.Alter();
  25.         $database.SetOffline()
  26.         Rename-Item -Path $InitialFilePath -NewName “$NewFilename”
  27.         $database.SetOnline()
  28.         Write-Host “File Renamed”
  29. }
  30. else
  31. {
  32.         Write-Host “Database does not exist”;
  33. }

This version will use the Server object to get the database that you are looking for so that you can get at the files.  The file you are looking for is the $logicalName so that you can get the PhysicalName of the file.  Then using the builtin CmdLets to handle Files operations, a Rename-Item is issued to rename the initial path to the new one.

Operation order:

  • ALTER DATABASE dbname SET OFFLINE
  • ALTER DATABASE dbname MODIFY FILE
  • Rename-Item from old to new
  • ALTER DATABASE dbname SET ONLINE

I did not put these in a function, but they could easily (or not too hard that is) put together using Parameters and make it versatile for other situations, but that is for you to do so that it works for your scenario.

Love PowerShell!

Leave a Reply