0

Lets say we have multiple invoke-sqlcmd statements like below:

invoke-sqlcmd -Query "PRINT 'Hello World 1'" -ServerInstance Server\SQL2017 -Database abc -Verbose
invoke-sqlcmd -Query "PRINT 'Hello World 2'" -ServerInstance Server\SQL2017 -Database abc -Verbose
invoke-sqlcmd -Query "PRINT 'Hello World 3'" -ServerInstance Server\SQL2017 -Database abc -Verbose

I want to do the following things:

  1. I want to log verbose output statements of multiple invoke-sqlcmd commands into a single log file
  2. Along with logging into the log file, can we show the verbose output in powershell window too? Also, can we manage it through any flag to show the output in powershell window or not?

Please suggest if it's possible or not.

2 Answers 2

0

For 1, you can use PowerShell redirection operators. As per Redirectable output streams, steam 4 is Verbose Stream which is what you want.

invoke-sqlcmd .... -verbose 4>> "D:\temp\yourlogfile.log"

Above will append all your invoke-sqlcmd into just one log file "D:\temp\yourlogfile.log"

your question part 2 is unclear to me.

2
  • When I redirect the verbose output to log file using >> "D:\temp\yourlogfile.log", I dont see the verbose output in powershell window also, which I want to. Is that possible? So basically I want to see the verbose output in both places, in log file as well as in powershell window. Commented Jan 12, 2021 at 16:11
  • You may want to implement custom PS function to do that - see this function and modify as per your needs. Commented Jan 12, 2021 at 17:11
0

For the First question, I agree with the above post of @Kin Shah. For the second question, which means if we want to log the verbose message into a log file and PowerShell window, both the places then we can use tee-object like below:

invoke-sqlcmd -Query "PRINT 'Hello World'" -ServerInstance WL357044\SQL2017 -Database abc -Verbose *>&1| tee-object -FilePath test.log -Append

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.