3

我正在尝试将 LAN 上的远程数据库备份到网络共享驱动器。我使用 .NET 使用相同的方法进行了测试,它确实有效,但我计划在没有 Jenkins 持续集成工具的情况下使用这个 PowerShell 脚本。

$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 

我没有收到任何异常,但也没有任何信息可以帮助我对代码进行故障排除。使用所有可用的 throw, catch, try 我只能得到。

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
4

1 回答 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.

于 2012-11-30T03:39:18.757 回答