Hi i am trying to write a powershell script to create a backup of mdf file(sqlserver database file) on windows7 ultimate machine
but when i run my script it show me error:
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
here is my code:
[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)
This code is work fine on xp machine but i want to run it on windows7 ultimate machine is there anything needs to be change ? Please suggest….
@Brandon has the right idea, but not quite up-to-date information. If you review this MSDN page on the sqlps utility you will see that it is deprecated, stating that “This feature will be removed in a future version of Microsoft SQL Server” in the second paragraph. That is indeed the case as of SQL Server 2012, which comes with the sqlps module built-in (as distinct from the sqlps utility). You can, as Brandon suggested make your own sqlps module but you do not need to because there is a version available for download at the end of this article. For the complete twisted, obfuscated, and gruesome tale of “sqlps” vs “sqlps” vs “sqlps” see Practical PowerShell for SQL Server Developers and DBAs, part 1 and part 2, that were just published on Simple-Talk.com. You will also find a link there for my wallchart that puts everything you need to know from both articles onto a single sheet.