【问题标题】:Create backup of sqlserver database using powershell script on windows7在 windows7 上使用 powershell 脚本创建 sqlserver 数据库的备份
【发布时间】:2012-08-23 12:11:56
【问题描述】:

您好,我正在尝试编写一个 powershell 脚本来在 windows7 终极机器上创建 mdf 文件(sqlserver 数据库文件)的备份

但是当我运行我的脚本时它显示错误:

New-Object : Cannot find type [Microsoft.SqlServer.Management.Smo.Backup]: make sure the assembly
is loaded.
At C:\Users\abc\tmp.ps1:5 char:23
+ $dbBackup = new-object <<<<  ("Microsoft.SqlServer.Management.Smo.Backup")
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Property 'Database' cannot be found on this object; make sure it exists and is settable.
At C:\Users\abc\tmp.ps1:8 char:11
+ $dbBackup. <<<< Database = "ds"
    + CategoryInfo          : InvalidOperation: (Database:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Users\abc\tmp.ps1:11 char:28
+ $dbBackup.Devices.AddDevice <<<< ("D:\backups\BLACKSWASTIK.bak", "File")
    + CategoryInfo          : InvalidOperation: (AddDevice:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Property 'Action' cannot be found on this object; make sure it exists and is settable.
At C:\Users\abc\tmp.ps1:14 char:11
+ $dbBackup. <<<< Action="Database"
    + CategoryInfo          : InvalidOperation: (Action:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Users\abc\tmp.ps1:17 char:20
+ $dbBackup.SqlBackup <<<< ($s)
    + CategoryInfo          : InvalidOperation: (SqlBackup:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

这是我的代码:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "localhost\sqlexpress2008"


$dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")

#Set the Database property to Northwind 
$dbBackup.Database = "bs"

#Add the backup file to the Devices collection and specify File as the backup type 
$dbBackup.Devices.AddDevice("D:\backups\BLACKSWASTIK.bak", "File")

#Specify the Action property to generate a FULL backup 
$dbBackup.Action="Database"

#Call the SqlBackup method to generate the backup 
$dbBackup.SqlBackup($s)

这段代码在 xp 机器上运行良好,但我想在 windows7 终极机器上运行它,有什么需要改变的吗?请建议....

【问题讨论】:

    标签: sql-server-2008 powershell windows-7


    【解决方案1】:

    @Brandon 有正确的想法,但不是最新的信息。如果您查看sqlps utility 上的 MSDN 页面,您会看到它已被弃用,并在第二段中指出“此功能将在 Microsoft SQL Server 的未来版本中被删除”。这确实是 SQL Server 2012 的情况,它带有内置的 sqlps module(与 sqlps utility 不同)。您可以按照 Brandon 的建议制作自己的 sqlps 模块,但您不需要这样做,因为在 this article 末尾有一个可供下载的版本。有关“sqlps”与“sqlps”与“sqlps”的完整扭曲、混淆和可怕的故事,请参阅适用于 SQL Server 开发人员和 DBA 的实用 PowerShellpart 1part 2,这只是发表在 Simple-Talk.com 上。您还可以在此处找到我的挂图的链接,该链接将您需要从两篇文章中了解的所有内容都放在一张纸上。

    【讨论】:

      【解决方案2】:

      查看this article from MSDN 运行 SQL Server Powershell。它为您提供了先决条件以及一个不错的脚本,您可以运行加载所有必需的程序集和命名空间。然后,您可以将其 DotSource 到您的脚本中,以便您访问 SMO。

      对于我的环境,我使用了这个脚本,稍微调整了一下,并创建了一个PowerShell module,以便更容易地将代码引入我的脚本中。

      【讨论】:

        猜你喜欢
        • 2018-07-23
        • 1970-01-01
        • 2017-01-28
        • 1970-01-01
        • 2022-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多