3

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 

image

image

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)

Error message: error message

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

2
  • 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 Commented Jul 22 at 20:27
  • At a minimum, you need to run this manually, split the "filemapping" into a separate variable, and confirm the command that you think is being entered is actually being entered. Commented Jul 22 at 20:27

1 Answer 1

4

There's a space after the backtick on the -Database line, which is breaking your code.

Most people won't recommend the use of backticks to break up a oneliner in a more readable format. Instead you could use something like this. I believe its called splatting:

 Set-DbatoolsInsecureConnection -SessionOnly $mapping = @{ "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" } $arguments = @{ SqlInstance = 'BeeSQL3\BEESQL2022' WithReplace = $true Database = "WH_BEE_New" FileMapping = $mapping } Get-ChildItem "F:\WH_BEE\FULL\WH_BEE_FULL_$(Get-Date -Format "yyyyMMdd")_*.bak" | Restore-DbaDatabase @arguments 
6
  • Thanks for help. I am getting this error "A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'. Do I have to modify where it says '$arguments'? Commented Jul 21 at 18:42
  • 1
    my bad: when passing arguments with this method the splatting variable must begin with an @ (at), not a $ (dollar). It literally says it in the first paragraph of the documentation. I did still miss it. :) I fixed it in my answer. Commented Jul 22 at 7:38
  • Thank you. I am facing other issue now. This is error message: "No backups passed through. This could mean the SQL instance cannot see the reference files, the file's headers could not be read or some other issue." I updated the original post with error message in an image. Thanks. Commented Jul 22 at 18:09
  • 1
    1: Is F:\ a network share? If so, It would not be visible for the instance just as such. 2. Did you try with a fixed file name for the .bak-file and did it work? Commented Jul 23 at 5:54
  • Thank you again. 1. Yes F:\ is a network share. What should I do to make it work? Should I change to other drive? Should I focus on permission issue on F:\ drive then? 2. I actually have 6 files (not just one) that I have to ingest that would match with format $(Get-Date -Format "yyyyMMdd")_*.bak. If that is the case, do I have to modify code? Thanks. Commented Jul 23 at 17:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.