当我在 SQL Server 计算机上本地运行以下脚本行时,它完美执行:
Backup-SqlDatabase -ServerInstance $serverName -Database $sqldbname -BackupFile "$($backupFolder)$($dbname)_db_$($addinionToName).bak"
$serverName 包含 SQL Server 实例的简称。SQL Server 是 2012,因此这些新 cmdlet 运行良好。
另一方面,当我尝试从这样的 TeamCity 代理机器执行 DB 备份时(通过 Invoke-Command cmdlet):
function BackupDB([String] $serverName, [String] $sqldbname, [String] $backupFolder, [String] $addinionToName)
{
Import-Module SQLPS -DisableNameChecking
Backup-SqlDatabase -ServerInstance $serverName -Database $sqldbname -BackupFile "$($backupFolder)$($dbname)_db_$($addinionToName).bak"
}
Invoke-Command -computername $SQLComputerName -Credential $credentials -ScriptBlock ${function:BackupDB} -ArgumentList $SQLInstanceName, $DatabaseName, $BackupDirectory, $BakId
导致错误:
无法连接到服务器 $serverName。+ CategoryInfo : NotSpecified: (:) [Backup-SqlDatabase], ConnectionFailureException + FullyQualifiedErrorId : Microsoft.SqlServer.Management.Common.ConnectionFailureException,Microsoft.SqlServer.M anagement.PowerShell.BackupSqlDatabaseCommand
远程执行 Backup-SqlDatabase cmdlet 的正确方法是什么?