I have a PowerShell script producing the error below:
Set-DbatoolsInsecureConnection -SessionOnly Get-ChildItem "F:\WH_BEE\FULL\WH_BEE_FULL_$(Get-Date -Format "yyyyMMdd")_*.bak" | ` Restore-DbaDatabase ` -SqlInstance 'BeeSQL3\BEESQL2022' ` -WithReplace ` -Database WH_BEE_New ` -FileMapping @{ "WH_Data1_New" = "F:\WH_Test\WH_BEE_New.MDF"; "WH_Data2_New" = "F:\WH_Test\WH_BEE_New.NDF"; "WH_Log_New" = "F:\WH_Test\WH_BEE_New.LDF" };
This is error message:
..MDF already exists on ... and owned by another database, cannot restore ..NDF already exists on ... and owned by another database, cannot restore ..LDF already exists on ... and owned by another database, cannot restore
I have another SQL database (called "WH_BEE") to restore database daily using T-SQL script (instead of PowerShell) using same BAK files in same SQL Server, but I am not sure how this PowerShell would point to following locations (for mdf, ldf and ndf as shown below) because they were not even mentioned in this script.
F:\data2022\WH_BEE.MDF F:\data2022\WH_BEE_1.NDF G:\log2022\HW_BEE.LDF
I read this thread and it mentioned about "WITH MOVE" and "MOVE".
Is this something I could apply to existing PowerShell script?
Update (7/22/2025)
sql.connection. trustcert True Trust SQL Server certificate sql.connection.encrypt False Encrypt connection to server -FileMapping: The term '-FileMapping is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At F:\WH BEE 071825.ps1:8 char:5
I am not sure how this PowerShell would point to following locations (for mdf, ldf and ndf as shown below) because they were not even mentioned in this script.
In addition to the other problems, the command is defaulting to a variant (there are multiple) that does not include the-FileMapping
parameter. Note the error:The term '-FileMapping is not recognized as the name of a cmdlet, function, script file, or operable program.
docs.dbatools.io/Restore-DbaDatabase.html