Detach/Attach

This category page is part of a series of Professional PowerShell SMO Recipes. You can find an index of all recipes on the main SMO Recipes page, and if you want to learn more about each recipe and see some sample screenshots, click on its category page.

These scripts were created and tested on Windows 8.1 with PowerShell v4 and SQL Server 2014, though most recipes should work with earlier versions of SQL Server. PowerShell v3 and above is required for many of the recipes. If you have a recipe request, leave a comment and I'll see what I can do. This cookbook will be continuously built, as I work more with SMO.

Recipe Categories

Detach/Attach

To see some these scripts used within a formally written PowerShell script that includes parameterized functions, check out my SQL migration script, Start-SQLMigration.ps1, on Microsoft’s ScriptCenter.

Detach Database

Warning! This code removes will break a mirror if one exists. It will also remove a database from an Availability Group if it is part of one. Be sure to note the database’s attributes such as owner chaining, trustworthy, broker enabled, read-only status, and dbowner. These settings are lost during attach/detaches.

Attach database to server from files contained within server default data and log locations.

Note that there are some database attributes (owner chaining, trustworthy, broker enabled, read-only, dbowner) that are lost when performing a detach/attach. Refer to my Start-SQLMigration.ps1 script to see how you can retain these settings when performing live migrations.

Attach Database, reusing previous file structure.

Note that there are some database attributes (owner chaining, trustworthy, broker enabled, read-only, dbowner) that are lost when performing a detach/attach.

Enumerate file structure within detached MDF database file

While researching how connect to a detached MDF and read the internal file structure of the internal files, including LDF files, I kept seeing suggestions for using the undocumented DBCC command DBCC checkprimaryfile. Initially, my code looked like this:

After some exploration, I discovered that this task can actually be accomplished purely in SMO using EnumDetachedDatabaseFiles and EnumDetachedLogFiles.

Get detached SQL Server MDF file information

Put it all together. You can find the formalized script of this snippet on ScriptCenter (Get-DetachedDBinfo.ps1).

get-detacheddbinfo

Want to see more? You can find an index of all recipes on SMO Recipes Index Page page or click on any specific category at the top of this page.

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Leave a Reply

Your email address will not be published. Required fields are marked *

*