最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - How to Display Entire Error Message from Invoke-Sqlcmd - Stack Overflow

programmeradmin0浏览0评论

Here is the relevant code:

        $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
        }

When I run this on the command line (PS shell), I get ONLY the following:

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 ''.

However, when I run the exact same code in my Azure pipeline, the logs there show the full message:

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 ''.

How do I get the full output (the entire error message) on the command line (PS shell)?

**Update: With help from @mklement0 I've updated my code to

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

this gives me the stack trace where the exception is Microsoft.SqlTools.ServiceLayer.BatchParser.BatchParserException

To me, this suggests that maybe the parser is getting the original response/error message from the query, trying to parse it, and then throwing its own exception, instead of relaying the original one. Is there anything to do about this?

发布评论

评论列表(0)

  1. 暂无评论