0

I currently have:

(invoke-sqlcmd -inputfile (Join-Path $FileDirectory $FileName) -ServerInstance $ServerName -verbose) > $OutFileName

To which I'm getting the output:

VERBOSE: Changed database context to 'BLUELABEL'

I'm trying to use the verbose output to write to the file designated by the Join-Path function.

What am I doing wrong?

(Using PowerShell V3)

I did find this question with answers, but it seems different and I can't seem to apply the same ideas: Powershell Invoke-Sqlcmd capture verbose output

Community
  • 1
  • 1
obizues
  • 1,473
  • 5
  • 16
  • 30
  • **What am I doing wrong?** Not giving `$OutFileName` a value. Question is, why do you want to write to the file that contains the sql command you're executing? – arco444 May 19 '15 at 15:14
  • I'm sorry this is just the portion that I output on, please assume all variables are properly assigned. – obizues May 19 '15 at 15:49

2 Answers2

1

Verbose output is written to a different stream than regular output. The > operator redirects only the success output stream. To redirect the verbose output stream you need

(...) 4> $OutFileName

or

(...) 4>&1 > $OutFileName

if you want to combine success and verbose output streams.

See about_Redirection for further information.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Trying both of the suggested solutions I get the following error: `4 : Unexpected token '4' in expression or statement. + CategoryInfo : ParserError: (4:String) [], ParseException + FullyQualifiedErrorId : UnexpectedToken` – obizues May 19 '15 at 15:46
1

I ended up getting

sqlps -Command "&{invoke-sqlcmd -inputfile (Join-Path $PSScriptRoot $file) -ServerInstance $server -verbose}" *> "$PSScriptRoot\SqlLog_$file`_$(get-date -f yyyy-MM-dd-hh-mm-ss).txt"

to work.

Originally I was calling another script and passing in 3 variables to it, and running the invoke-sqlcmd call. For some reason that caused me to be unable to use the "*>" or any character combined with the ">".

Maybe someone else can elaborate on why exactly this is the case.

obizues
  • 1,473
  • 5
  • 16
  • 30