Shachaf.Gortler
Shachaf.Gortler

Reputation: 5735

sqlcmd not returning error

I am running the following sqlcmd via power shell

$dump = sqlcmd -S $server -Q $sqlCommand -t $queryTimeout -b -h -1 -W

I trying the write the output to screen

$message = "Error while executing sql {0}, Error details {1}" -f "$sqlCommand","$dump" 
Write-Warning $message

but $dump is empty

Upvotes: 0

Views: 680

Answers (1)

Matt
Matt

Reputation: 46710

I am not familiar with sqlcmd.exe but from the description of your expectations I think that sqlcmd is sending information down the error stream. This is a seemingly odd but common practice and therefore occurrence.

Problem here is that the variable $dump will only collect information send to the output stream. What you can do is redirect the error stream to output stream with a redirector. For more information you can look at about_redirection

So using the following will accomplish that:

$dump = sqlcmd -S $server -Q $sqlCommand -t $queryTimeout -b -h -1 -W 2>&1

The linked document describes 2>&1 as

Sends errors (2) and success output (1) to the success output stream.

Now $dump should contain what you are looking for. Be careful though as it might contain more information than you expect.

Upvotes: 1

Related Questions