PowerShell for Copy Database
This is PowerShell tutorial script to understand how to copy existing database with PowerShell and SMO. We will be using Transfer Class from SMO. If you wish to just generate the copy script then there is an option to just script out the transfer using the ScriptTransfer method. When we are ready to bring the data and schema over, we can use the TransferData method.
Here is PowerShell Script to Copy SQL Server Database
#import SQL Server module Import-Module SQLPS -DisableNameChecking #your SQL Server Instance Name $SQLInstanceName = "SQL2012" $Server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $SQLInstanceName #provide your database name which you want to copy $SourceDBName = "TestDB" #create SMO handle to your database $SourceDB = $Server.Databases[$SourceDBName] #create a database to hold the copy of your source database $CopyDBName = "$($SourceDBName)_copy" $CopyDB = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Database -ArgumentList $Server , $CopyDBName $CopyDB.Create() #Use SMO Transfer Class by specifying source database #you can specify properties you want either brought over or excluded, when the copy happens $ObjTransfer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer -ArgumentList $SourceDB $ObjTransfer.CopyAllTables = $true $ObjTransfer.Options.WithDependencies = $true $ObjTransfer.Options.ContinueScriptingOnError = $true $ObjTransfer.DestinationDatabase = $CopyDBName $ObjTransfer.DestinationServer = $Server.Name $ObjTransfer.DestinationLoginSecure = $true $ObjTransfer.CopySchema = $true #if you wish to just generate the copy script #just script out the transfer $ObjTransfer.ScriptTransfer() #When you are ready to bring the data and schema over, #you can use the TransferData method $ObjTransfer.TransferData()
Once you are done with successful execution of this PowerShell Script, Check whether you database is copied or not. Go to SQL Server Management Studio and check the user databases in Object Explorer. You may need to refresh Databases folder from Object Explorer.
Hope this helps.
Let me know if you have any queries on this PowerShell Tutorial.
Thanks & Regards,
Sandip
I just want to mention I am just beginner to blogging and seriously loved this web page. Most likely I’m planning to bookmark your website . You surely have perfect well written articles. Thank you for sharing with us your website page.
This website was… how do I say it? Relevant!
! Finally I’ve found something that helped me. Kudos!
When someone writes an article he/she maintains the thought of a
user in his/her mind that how a user can know it. So that’s why this post is amazing. Thanks!
Hello there! Would you mind if I share your blog with my facebook group?
There’s a lot of folks that I think would really appreciate your content. Please let me know. Thanks
I know this web page presents quality based posts and additional stuff, is there any other web page which provides these data in quality?
I was recommended this website by my cousin.
I’m not sure whether this post is written by him as nobody else know such detailed about my difficulty. You’re
incredible! Thanks!
Hola! I’ve been following your blog for a long time now and finally got the courage to go ahead and give you a shout out from New Caney Texas! Just wanted to say keep up the excellent work!
Excellent site you have here.. It’s hard to find high quality writing like yours nowadays. I really appreciate people like you! Take care!!
Oh my goodness! Awesome article dude! Thank you so much, However I am experiencing problems with your RSS. I don’t know why I am unable to join it. Is there anybody getting similar RSS problems? Anyone that knows the answer can you kindly respond? Thanx!!
Ahaa, its pleasant conversation about this piece of
writing here at this web site, I have read all that, so at this time me
also commenting at this place.
We absolutely love your blog and find many of your post’s to be just what I’m looking for. can you offer guest writers to write content in your case? I wouldn’t mind creating a post or elaborating on a number of the subjects you write related to here. Again, awesome web site!
Exception calling “TransferData” with “0” argument(s): “An error occurred while transferring data. See the inner exception for details.”
At line:36 char:1
+ $ObjTransfer.TransferData()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TransferException
This is still valid, and works like a charm for CI / CD where database has to come up and be put to death automatically