Powershell 导入SQLPS模块并使用它将数据库备份和恢复到不同的服务器
我正在从未安装SQLPS(Ms SQL Server数据库引擎)的客户端计算机(Win7)运行PS脚本 因此,我通过导入远程ps会话来实现:Powershell 导入SQLPS模块并使用它将数据库备份和恢复到不同的服务器,powershell,sqlps,Powershell,Sqlps,我正在从未安装SQLPS(Ms SQL Server数据库引擎)的客户端计算机(Win7)运行PS脚本 因此,我通过导入远程ps会话来实现: 创建到SQL Server的远程PS会话 $sourceSQLRemoteSession=新PSSession-ComputerName$SQLServerHostname-Authentication协商-Credential$PSOCredentialObject 导入SQLPS模块 导入PSSession-会话$sourceSQLRemoteSess
$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
最好的方法是什么
谢谢您最好的方法是使用。过来看!您可以从PowerShell Gallery进行安装,但如果您运行的是Windows 7,则可能需要将PowerShell升级到v3或更高版本(go for)才能使用PowerShell Gallery dbaTools有一个用于备份和恢复DBs的命令 下面是他们的一个例子: 希望有帮助
Copy-DbaDatabase -Source sqlserver2014a -Destination sqlcluster -Exclude Northwind, pubs, AdventureWorks -BackupRestore -NetworkShare \\\\fileshare\sql\migration