I am running a SQL stored procedure via invoke-sqlcmd, having read the posts on Powershell Invoke-Sqlcmd capture verbose output and How to run a PowerShell script with verbose output?.
I have a working solution, however I am struggling with a tweak so that the solution can be complete. It currently prints onto the console, I only want the output to be in a variable and no console output. I have also toyed with the idea of setting $VerbosePreference = "SilentlyContinue"
to no avail.
Here is what I have.
$null = invoke-sqlcmd -ServerInstance "DB_Server" -Database "DB" -Query "sp_storedProcedure_with_prints" -OutputSQLErrors $true -ErrorAction Stop -ErrorVariable error_var -verbose 4>&1 | Tee-Object -variable script_output_variable
It works, it does what I want it to do which is to save the script output in the variable $script_output_variable what is happening now is that the hundreds of lines that the stored procedure outputs is not required, especially when the script runs on thousands of servers.