Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8395123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:13:11+00:00 2026-06-09T20:13:11+00:00

I am trying to create a Powershell script to restore database to my laptop

  • 0

I am trying to create a Powershell script to restore database to my laptop from my desktop. I have a script which creates the backup files and have almost got the restore script working apart from a strange error I get with a CDC enabled database. What I end up with is an off-line single user database. I have to bring it back on-line and change it back to multi-user manually. Here are the relevant bits of my powershell code …

$instance = "(local)"
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $instance

$restore = New-Object Microsoft.SqlServer.Management.Smo.Restore
$restore.Action = "Database"
$restore.Database = $dbname
$restore.NoRecovery = $false
$restore.ReplaceDatabase = $true
$restore.Devices.AddDevice($filename, "File")
$restore.SqlRestore($server)

I get an error message saying …

*System.Data.SqlClient.SqlException: Could not update the metadata that indicates database xxxxx is not enabled for Change Data Capture. The failure occurred when executing the command ‘[sys].[sp_MScdc_ddl_database triggers ‘drop”. The error returned was 15517: ‘Cannot execute as the database principal because the principal “dbo” does not exist, this type of principal cannot be impersonated, or you do not have permission’*

and a bit further down …

*The database has been left offline. See the topic MSSQL_ENG003165 in SQL Server Books Online.*

further down …

Converting database ‘xxxxx’ from version 655 to the current version 661.
Database ‘xxxxx’ running the upgrade step from version 655 to version 660.
Database ‘xxxxx’ running the upgrade step from version 660 to 661.

While I can get the database back to a useable state, I would ideally like to have it completely scripted. The idea of this is that I can run the backup script on my desktop and then run the restore script on my laptop, which then restores the databases on my laptop so I have a working copy of the same database for when I need to work remotely.

Any insights would be great, even better if someone has come across and solved the same problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:13:12+00:00Added an answer on June 9, 2026 at 8:13 pm

    When I had to set an explicit CDC setting in a restore script, I did something like:

        $script_lines = $restore.script( $server )
        $script_lines += ', keep_cdc'
        $script = ''
        foreach ($line in $script_lines) {
                $script += $line
        }
    
        $script
        invoke-sqlcmd -ServerInstance $server.name -Query $script -QueryTimeout 65535
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a PowerShell script that creates a new IIS 6
What I'm trying to do is create a PowerShell script which gets a list
I'm trying to write a custom powershell script that will create a local user
I'm trying to run the following PowerShell script to create a local user. This
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I have 2 powershell scripts that I execute from c# which I'm using to
I am trying to create a empty database in SQL server using powershell and
To piggyback onto this question, PowerShell script to delete files from list and output
I am trying to use WPF printing from PowerShell. I have found a simple
I'm trying to use CreateProcess to launch a powershell script from within my application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.