sql-server - 使用 Restore-SqlDatabase cmdlet 时无法绑定(bind) RelocateFile

标签 sql-server powershell smo

我的计算机上安装了 SQL Server 2012 Developer。我还安装了 SQL Server 2014 管理对象,这可能是问题的根源。

我正在编写一个模块,通过 Powershell 自动执行一些常见的开发任务。其中之一就是简单地将现有数据库复制到新数据库。

我知道有三种不同的备份和恢复方法:执行 SQL 语句、SMO 对象和 SQLPS cmdlet。我对 SQLPS 路线感兴趣。这是我的功能:

push-location
import-module sqlps -disablenamechecking
pop-location

function Copy-Database {
    param (
        [string] $database,
        [string] $newDatabase
    )

    $backupFile = "$database-{0:yyyyMMddhhmmss}.bak" -f (get-date)
    backup-sqldatabase -serverinstance $defaultServerInstance -database $database -backupfile $backupFile -copyonly

    $solutionName = $newDatabase

    $mdf = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("MyDb_Data", "$defaultDatabaseRootPath\$solutionName\$newDatabase.mdf")
    $ldf = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("MyDb_Log", "$defaultDatabaseRootPath\$solutionName\$newDatabase.ldf")

    restore-sqldatabase -serverinstance $defaultServerInstance -database $newDatabase -backupfile $backupFile -RelocateFile @($mdf,$ldf)
}

它备份数据库,但是当它尝试恢复时,我收到以下错误:

Restore-SqlDatabase : Cannot bind parameter 'RelocateFile'. Cannot convert the "Microsoft.SqlServer.Management.Smo.RelocateFile" value of type "Microsoft.SqlServer.Management.Smo.RelocateFile" to type "Microsoft.SqlServer.Management.Smo.RelocateFile".

此处也描述了此问题:Problems with RelocateFile property in the Restore-SqlDatabase cmdlet

我承认问题可能是程序集中的冲突。接受的答案提供了两个建议:

  1. Make sure that the versions match.
  2. Use the Microsoft.SqlServer.Management.Smo.Restore.SqlRestore method instead of the Restore-SqlDatabase cmdlet.

但是,他们只解释了如何执行#2。我想知道如何使用 Restore-SqlDatabase cmdlet 使其正常工作。

最佳答案

我知道这已经很旧了,但我也遇到过同样的问题。

PS C:\> [AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | ? FullName -eq Microsoft.SqlServer.Management.Smo.RelocateFile | select Assembly

组装

  • Microsoft.SqlServer.SmoExtended, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91

  • Microsoft.SqlServer.SmoExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91

New-Object "Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" (*LogicalName*, *PhysicalName*)

这样做,使用完全限定的类型名称,可以解决问题。

希望这会对发现此问题的其他人有所帮助。

关于sql-server - 使用 Restore-SqlDatabase cmdlet 时无法绑定(bind) RelocateFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26400752/

相关文章:

sql-server - 如何找到缺失的行?

c# - 使用 SMO DependencyWalker 加载 TreeView

windows - 当 PowerShell 脚本更新时,Doxygen 不读取 Doxyfile

powershell - powershell GCI递归地跳过文件夹和某些文件类型

powershell - 从 Powershell 调用带有非常长的可变参数列表的程序?

sql-server - 无法让 powershell 处理 SQL Server 连接失败

c# - 使用 SMO 在 SQL Azure 数据库中创建表失败

mysql - SQL-如何在 NOT EXIST 语句中显示以 3 秒开头的每个帐户的结果(例如 300,301, 302..)?

sql-server - 顺序引导和碎片

sql - 具有相同外键的 INT 值之和