我正在从未安装SQLPS(Ms SQL Server数据库引擎)的客户端计算机(Win7)运行PS脚本。
因此,我通过导入远程ps会话来实现:
$ sourceSQLRemoteSession = New-PSSession -ComputerName $ SQLServerHostname -Authentication negotiate -Credential $ PSOCredentialObject
Import-PSSession -Session $ sourceSQLRemoteSession -Module SQLPS -DisableNameChecking
Backup-SqlDatabase -ServerInstance" destinationSQL \ SQLInstance" -Database" blabla_db" -BackupFile" c:\ blabla.bak" -Credential $ PSOCredentialObject
问题是,如果我需要将数据库还原到不同的SQL数据库服务器,则需要我使用" RelocateFile"对象,例如:
$RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("uat_project1_db", "c:\SQLDATA\blabla.mdf")
$RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("uat_project1_db_Log", "c:\SQLDATA\blabla.ldf")
Restore-SqlDatabase -ServerInstance "Computer\Instance" -Database "MainDB" -BackupFile "\\mainserver\databasebackup\MainDB.trn" -RelocateFile @($RelocateData,$RelocateLog)
当我运行新对象时,它似乎不喜欢它:
错误:
New-Object : Cannot find type [Microsoft.SqlServer.Management.Smo.RelocateFile]: verify that the assembly containing this type is loaded.At line:1 char:17
+ ... ocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
最好的方法是什么?
由于
答案 0 :(得分:1)
您最好的方法是使用dbaTools。看看这个!您可以从PowerShell库安装,但如果您运行的是Windows 7,则可能需要将PowerShell升级到v3或更高版本(转到v5!)才能使用PowerShell库。
dbaTools有一个用于备份和恢复数据库的命令。
以下是documentation的一个例子:
Copy-DbaDatabase -Source sqlserver2014a -Destination sqlcluster -Exclude Northwind, pubs, AdventureWorks -BackupRestore -NetworkShare \\\\fileshare\sql\migration
希望有所帮助