3

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
1
  • My main question would be: does the user / account that executes this script have access to that remote location? Could you attempt to just copy a file to that location and see if that works? Also: does this backup script work if you target a local drive on the SQL Server machine?? Commented Nov 29, 2012 at 21:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i do know so many scripting for linux that learning ps is a little complicated. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.