Search code examples
.netsql-server-2008powershellsmo

Powershell SQL Server 2008 Database Backup Remotely


I am trying to backup a remote database on the LAN to a network shared drive. I tested using the same approach using .NET and it did work but i am planning to use this PowerShell script withing out Jenkins Continuous Integration tool.

$dbserver = "dbserver"
$location = "\\otherserver\Temp\"
$user = "user"
$pwd = "password"

$timestamp=((get-date).toString("yyyy_MM_dd_hh_mm"))
$file = $location + "jira_" + $timestamp + ".bak"

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null

$connection = New-Object "Microsoft.SqlServer.Management.Common.ServerConnection" 
$connection.ServerInstance =$dbserver
$connection.LoginSecure = $false
$connection.Login = $user 
$connection.Password = $pwd

$server = New-Object "Microsoft.SqlServer.Management.Smo.Server" $connection
$backup = New-Object "Microsoft.SqlServer.Management.Smo.backup"
$backup.Action = 'Database'

$device = New-Object ('Microsoft.SqlServer.Management.Smo.BackupDeviceItem') ($file, 'File')
#$device.DeviceType = 'File'
#$device.Name = $file

$backup.MediaDescription = "Disk"
$backup.Database= "jira"
$backup.Devices.Add($device)
$backup.SqlBackup 

I don't get any exceptions but also I don't get any information that can help me troubleshoot my code. Using all available throw, catch, try I can only get.

MemberType          : Method
OverloadDefinitions : {System.Void SqlBackup(Microsoft.SqlServer.Management.Smo.Server srv)}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : System.Void SqlBackup(Microsoft.SqlServer.Management.Smo.Server srv)
Name                : SqlBackup
IsInstance          : True

Solution

  • SqlBackup is a method. When you call a method you need to include opening and closing parenthesis:

    $backup.sqlbackup($server)
    

    Omitting the parens will output the method signature instead of calling the method. Omitting the method signature is not an error hence no error.