在混合模式下使用PowerShell脚本复制SQL Server数据库

时间:2019-03-20 07:40:18

标签: sql-server powershell

我想使用使用用户名和密码的PowerShell脚本创建SQL Server数据库。我使用了以下脚本:

$SQLInstanceName = "(localdb)\MSSQLLocalDB"
$Server  = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $SQLInstanceName 

# This sets the connection to mixed-mode authentication 
$Server.ConnectionContext.LoginSecure=$false; 

# This sets the login name 
$Server.ConnectionContext.set_Login("test"); 

# This sets the password 
$Server.ConnectionContext.set_Password("*******")  
$SourceDBName   = "@@SourceDBName@@" 
$SourceDB = $Server.Databases[$SourceDBName] 
$CopyDBName = "@@DestDBName@@"
$CopyDB = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Database -ArgumentList $Server , $CopyDBName
$CopyDB.Create() 
$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 
$ObjTransfer.ScriptTransfer() enter code here
$ObjTransfer.TransferData()

但是我得到这个错误:

  

数据库'coupadb_master_TestClient'的SetParent失败。

     

在System.Threading.Tasks.Task.ThrowIfExceptional(布尔值includeTaskCanceledExceptions)

0 个答案:

没有答案