嗨,我正在尝试编写一个 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 终极机器上运行它有什么需要改变的吗?请建议....