当从Powershell调用时,SQLCMD返回ExitCode == 1,即使成功

时间:2010-02-04 01:11:53

标签: sql sql-server windows powershell scripting

我有一个Powershell脚本,它调用sqlcmd来运行备份数据库的sql脚本:

function RunSqlScriptWithErrorMessage
{
    param([string]$sqlServer, [string]$sqlUserName, [string]$sqlPassword, [string]$scriptName, [string]$errorMessage)

    $commandName = "sqlcmd"
    $startInfo = New-Object Diagnostics.ProcessStartInfo($commandName)
    $startInfo.UseShellExecute = $false
    $startInfo.Arguments = "-S $sqlServer -U $sqlUserName -P $sqlPassword -i `"${sqlScriptName}`""
    $process = [Diagnostics.Process]::Start($startInfo)
    $process.WaitForExit()
    $exitCode = $process.ExitCode
    if($exitCode -ne 0 ) { throw $errorMessage}
}

奇怪的是,即使备份成功,process.ExitCode == 1也是如此。任何想法为什么会这样 - 如果成功,exitcode不应该是0吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

抱歉 - 这是一个错误。