[英]Import SQLPS module and use it to backup and restore DB to different server
I am running PS script from the client machine (Win7) that does not have SQLPS (Ms SQL Server Database Engine) installed. 我正在从未安装SQLPS(MS SQL Server数据库引擎)的客户端计算机(Win7)运行PS脚本。
Due to this, I do it through importing a remote ps-session: 因此,我通过导入远程ps会话来实现:
$sourceSQLRemoteSession = New-PSSession -ComputerName $SQLServerHostname -Authentication negotiate -Credential $PSOCredentialObject
$ sourceSQLRemoteSession = New-PSSession -ComputerName $ SQLServerHostname-身份验证-Credential $ PSOCredentialObject
Import-PSSession -Session $sourceSQLRemoteSession -Module SQLPS -DisableNameChecking
Import-PSSession-会话$ sourceSQLRemoteSession-模块SQLPS -DisableNameChecking
Backup-SqlDatabase -ServerInstance "destinationSQL\\SQLInstance" -Database "blabla_db" -BackupFile "c:\\blabla.bak" -Credential $PSOCredentialObject
Backup-SqlDatabase -ServerInstance“ destinationSQL \\ SQLInstance”-数据库“ blabla_db” -BackupFile“ c:\\ blabla.bak”-凭据$ PSOCredentialObject
The thing is, if I need to restore the DB to a different SQL database server, require me to use "RelocateFile" objects, for example: 问题是,如果需要将数据库还原到其他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)
And when I run the new object, it does not seem to like it: 当我运行新对象时,它似乎不喜欢它:
Error: 错误:
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
What's the best method to approach this? 解决此问题的最佳方法是什么?
Thanks 谢谢
Your best approach would be to use dbaTools . 最好的方法是使用dbaTools 。 Check it out!
看看这个! You can install from the PowerShell Gallery, though if you're running Windows 7 you may have to upgrade PowerShell to v3 or above (go for v5! ) to make use of the PowerShell Gallery.
您可以从PowerShell库中进行安装,尽管如果您正在运行Windows 7,则可能必须将PowerShell升级到v3或更高版本(适用于v5! )才能使用PowerShell库。
dbaTools has a single command for backing up and restoring DBs. dbaTools有一个用于备份和还原数据库的命令。
Here's an example from their documentation : 这是他们的文档中的一个示例:
Copy-DbaDatabase -Source sqlserver2014a -Destination sqlcluster -Exclude Northwind, pubs, AdventureWorks -BackupRestore -NetworkShare \\\\fileshare\sql\migration
Hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.