You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell命令行中显示Invoke-Sqlcmd的完整错误信息?

问题:PowerShell命令行中无法获取Invoke-Sqlcmd的完整错误输出

相关代码

$params = @{
    Query = $query
    ServerInstance = $serverInstance
    Database = $database
    Verbose = $true
    OutputSqlErrors = $true
    AbortOnError = $true
    IncludeSqlUserErrors = $true
}

try{
    $result = Invoke-Sqlcmd @params
    return $result
}catch{
    Write-Host $_.Exception.Message  # Display the error message
    Write-Host $_
    throw $_.Exception.Message
}

本地PowerShell命令行的错误输出

Incorrect syntax was encountered while parsing ''.
Incorrect syntax was encountered while parsing ''.
At [].psm1:56 char:13
+             throw $_.Exception.Message
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Incorrect synta...ile parsing ''.:String) [], RuntimeException
    + FullyQualifiedErrorId : Incorrect syntax was encountered while parsing ''.

Azure管道中的日志输出

Cannot open database "[dbName]" requested by the login. The login failed.
Incorrect syntax was encountered while parsing ''.
Login failed for user '[username]'.Incorrect syntax was encountered while parsing ''.
At [].psm1:55 char:13
+             throw $_.Exception.Message
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (Incorrect synta...ile parsing ''.:String) [], RuntimeException
+ FullyQualifiedErrorId : Incorrect syntax was encountered while parsing ''.

疑问

如何在PowerShell命令行中获取完整的错误输出?


更新内容

在帮助下,已将代码更新为:

}catch{
    Write-Host $_  # Display the error message
    $_ | Format-List * -Force | Out-Host
    throw
}

此时可看到堆栈跟踪,异常类型为Microsoft.SqlTools.ServiceLayer.BatchParser.BatchParserException。

这似乎表明解析器在获取查询的原始响应/错误信息后,尝试解析并抛出了自身的异常,而非转发原始异常。请问是否有解决办法?


内容的提问来源于stack exchange,提问作者Adam Winter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:22:32