Tsql 在PowerShell中捕获不同的sqlcmd exitcode以解决连接/数据问题

Tsql 在PowerShell中捕获不同的sqlcmd exitcode以解决连接/数据问题,tsql,powershell,error-handling,sqlcmd,exit-code,Tsql,Powershell,Error Handling,Sqlcmd,Exit Code,我正在从PowerShell调用sqlcmd以执行一个T-SQL脚本。当前我正在使用“:On Error exit”退出脚本,如果使用的数据违反约束等导致错误。这由PowerShell检测$SqlcmdProcess.ExitCode 1来处理 但是,如果数据库存在连接问题,sqlcmd也会给出ExitCode 1。有没有办法将:On Error ExitCode设置为1以外的值?我知道使用诸如:Exit(选择2)之类的方法来执行此操作,但我还是希望使用:On Error,这样我就不必重写脚本。

我正在从PowerShell调用sqlcmd以执行一个T-SQL脚本。当前我正在使用“:On Error exit”退出脚本,如果使用的数据违反约束等导致错误。这由PowerShell检测$SqlcmdProcess.ExitCode 1来处理


但是,如果数据库存在连接问题,sqlcmd也会给出ExitCode 1。有没有办法将:On Error ExitCode设置为1以外的值?我知道使用诸如:Exit(选择2)之类的方法来执行此操作,但我还是希望使用:On Error,这样我就不必重写脚本。

您可以在Powershell中使用Exit关键字。这里有一个例子

创建一个名为sqlcmdexit.ps1的脚本,其内容如下:

$result = sqlcmd -S"missing" -d master -Q "select @@servername"
if ($result[1] -like "*Error Locating Server/Instance Specified*" -or $result[1] -like "*Could not open a connection to SQL Server*") {
    exit 99
}
调用脚本并观察exist代码:

C:\Users\Public\bin>.\sqlcmdExit.ps1
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : A network-related or instance-specific error has occurred while
 establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and i
f SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout expired.

C:\Users\Public\bin>$LASTEXITCODE
99
我不知道有什么方法可以设置默认ExitCode。使用启动过程,您可以执行类似的操作:

$tempFile = [io.path]::GetTempFileName()
$exitCode = (Start-Process -FilePath "sqlcmd.exe" -ArgumentList @"
-S"missing" -d master -Q "select @@servername"
"@ -Wait -NoNewWindow -RedirectStandardOutput $tempFile -Passthru).ExitCode

if ($exitCode -eq 1) {
    $result = get-content $tempfile
    if ($result[1] -like "*Error Locating Server/Instance Specified*" -or $result[1] -like "*Could not open a connection to SQL Server*") {
        remove-item $tempFile
        Exit 99
    }
}
else {
    remove-item $tempfile
    Exit $exitCode
}

您可以在Powershell中使用exit关键字。这里有一个例子

创建一个名为sqlcmdexit.ps1的脚本,其内容如下:

$result = sqlcmd -S"missing" -d master -Q "select @@servername"
if ($result[1] -like "*Error Locating Server/Instance Specified*" -or $result[1] -like "*Could not open a connection to SQL Server*") {
    exit 99
}
调用脚本并观察exist代码:

C:\Users\Public\bin>.\sqlcmdExit.ps1
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : A network-related or instance-specific error has occurred while
 establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and i
f SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout expired.

C:\Users\Public\bin>$LASTEXITCODE
99
我不知道有什么方法可以设置默认ExitCode。使用启动过程,您可以执行类似的操作:

$tempFile = [io.path]::GetTempFileName()
$exitCode = (Start-Process -FilePath "sqlcmd.exe" -ArgumentList @"
-S"missing" -d master -Q "select @@servername"
"@ -Wait -NoNewWindow -RedirectStandardOutput $tempFile -Passthru).ExitCode

if ($exitCode -eq 1) {
    $result = get-content $tempfile
    if ($result[1] -like "*Error Locating Server/Instance Specified*" -or $result[1] -like "*Could not open a connection to SQL Server*") {
        remove-item $tempFile
        Exit 99
    }
}
else {
    remove-item $tempfile
    Exit $exitCode
}

谢谢,这非常有帮助,可以让我区分不同类型的错误。然而,在一个变量中捕获sqlcmd的结果需要一些重写,因为我目前正在通过启动进程调用sqlcmd(对不起,应该提到这一点)。您知道是否有一种方法可以对流程对象执行类似的操作吗。据我所知,它只有ExitCode和ExitTime属性。或者,更可能的情况是,是否有一种方法可以在T-SQL中设置默认ExitCode on error?谢谢!我不确定我会那样做,但我想你已经给了我所需要的一切。干杯。谢谢,这非常有帮助,可以让我区分不同类型的错误。然而,在一个变量中捕获sqlcmd的结果需要一些重写,因为我目前正在通过启动进程调用sqlcmd(对不起,应该提到这一点)。您知道是否有一种方法可以对流程对象执行类似的操作吗。据我所知,它只有ExitCode和ExitTime属性。或者,更可能的情况是,是否有一种方法可以在T-SQL中设置默认ExitCode on error?谢谢!我不确定我会那样做,但我想你已经给了我所需要的一切。干杯