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
1 Answer 1
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.
Comments
Explore related questions
See similar questions with these tags.
$dump = sqlcmd -S $server -Q $sqlCommand -t $queryTimeout -b -h -1 -W 2>&1... maybe-iand give a filename, but I could only get it working with& (SQLCMD) -s localhost -x -i $file. Try removing all your switches, and if you get a result start adding them back.