Sql server 2008 在windows7上使用powershell脚本创建sqlserver数据库的备份
您好,我正在尝试编写powershell脚本,以便在windows7 ultimate计算机上创建mdf文件(sqlserver数据库文件)的备份 但当我运行脚本时,它会显示错误:Sql server 2008 在windows7上使用powershell脚本创建sqlserver数据库的备份,sql-server-2008,powershell,windows-7,Sql Server 2008,Powershell,Windows 7,您好,我正在尝试编写powershell脚本,以便在windows7 ultimate计算机上创建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 = ne
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
这段代码在xp机器上运行良好,但我想在windows7终极机器上运行它。有什么需要更改的吗?请建议……检查是否运行SQL Server Powershell。它为您提供了先决条件以及一个很好的脚本,您可以运行该脚本来加载所有必需的程序集和名称空间。然后,您可以将其点源代码添加到脚本中,以便访问SMO
对于我的环境,我采用了这个脚本,对它进行了一些调整,并创建了一个新的脚本,以使代码更容易地引入到我的脚本中。@Brandon的想法是正确的,但不是最新的信息。如果在上查看此MSDN页面,您将看到它已被弃用,并在第二段中声明“此功能将在Microsoft SQL Server的未来版本中删除”。SQL Server 2012就是这种情况,它内置了sqlps模块(与sqlps实用程序不同)。正如Brandon所建议的,您可以创建自己的sqlps模块,但您不需要这样做,因为在本教程的末尾有一个版本可供下载。有关“sqlps”与“sqlps”与“sqlps”的完全扭曲、模糊和可怕的故事,请参见刚刚在Simple-Talk.com上发布的《SQL Server开发人员和DBA实用PowerShell》。你还可以在我的挂图上找到一个链接,它将两篇文章中你需要知道的所有内容都放在一张纸上
[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)