Thor Logo dbatools

Mount-DbaDatabase

View Source
Chrissy LeMaire (@cl), netnerds.net
Windows, Linux, macOS

Synopsis

Attaches detached database files to a SQL Server instance

Description

Attaches detached database files (.mdf, .ldf, .ndf) back to a SQL Server instance, making the database available for use again. When database files exist on disk but the database is not registered in the SQL Server instance, this command reconnects them using the SQL Server Management Objects (SMO) AttachDatabase method.

If you don’t specify the file structure, the command attempts to determine the correct database files by examining backup history for the most recent full backup. This is particularly useful when restoring databases from file copies or moving databases between instances where the files already exist but need to be reattached.

Syntax

Mount-DbaDatabase
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [-Database] <String[]>
    [[-FileStructure] <StringCollection>]
    [[-DatabaseOwner] <String>]
    [[-AttachOption] <String>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> $fileStructure = New-Object System.Collections.Specialized.StringCollection
PS C:\> $fileStructure.Add("E:\archive\example.mdf")
PS C:\> $filestructure.Add("E:\archive\example.ldf")
PS C:\> $filestructure.Add("E:\archive\example.ndf")
PS C:\> Mount-DbaDatabase -SqlInstance sql2016 -Database example -FileStructure $fileStructure

Attaches a database named “example” to sql2016 with the files “E:\archive\example.mdf”, “E:\archive\example.ldf” and “E:\archive\example.ndf”. The database owner will be set to sa and the attach
option is None.

Example: 2
PS C:\> Mount-DbaDatabase -SqlInstance sql2016 -Database example

Since the FileStructure was not provided, this command will attempt to determine it based on backup history. If found, a database named example will be attached to sql2016.

Example: 3
PS C:\> Mount-DbaDatabase -SqlInstance sql2016 -Database example -WhatIf

Shows what would happen if the command were executed (without actually performing the command)

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

PropertyValue
Alias
RequiredTrue
Pipelinetrue (ByValue)
Default Value
-Database

Specifies the names of the detached databases to attach to the SQL Server instance.
Use this when you have database files (.mdf, .ldf, .ndf) on disk but the database is no longer registered in SQL Server.

PropertyValue
Alias
RequiredTrue
Pipelinefalse
Default Value

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-FileStructure

Specifies the complete collection of database file paths (.mdf, .ldf, .ndf) required to attach the database.
When omitted, the command attempts to determine file locations automatically using backup history from the most recent full backup.
Use this parameter when files are in non-standard locations or when automatic detection fails.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-DatabaseOwner

Sets the login account that will own the attached database.
When not specified, defaults to the sa account or the SQL Server sysadmin with ID 1 if sa is not available.
Use this to assign ownership to a specific login for security or administrative requirements.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-AttachOption

Controls how SQL Server handles the database attachment process and Service Broker configuration.
Use ‘RebuildLog’ when transaction log files are missing or corrupt, ‘EnableBroker’ to activate Service Broker, or ‘NewBroker’ to create a new Service Broker identifier.
Defaults to ‘None’ for standard attachment without special handling.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueNone
Accepted ValuesNone,RebuildLog,EnableBroker,NewBroker,ErrorBrokerConversations
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with “sea of red” exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this “nice by default” feature off and enables you to catch exceptions with your own try/catch.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-WhatIf

If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

PropertyValue
Aliaswi
RequiredFalse
Pipelinefalse
Default Value
-Confirm

If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.

PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value