diff --git a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 index 2a58aa82f..d8709708c 100644 --- a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 +++ b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 @@ -88,7 +88,25 @@ function Invoke-SqlQueryDeployment $additionalArguments = EscapeSpecialChars $additionalArguments Write-Verbose "Invoke-SqlCmd arguments : $commandToLog $additionalArguments" - Invoke-Expression "Invoke-SqlCmd @spaltArguments $additionalArguments" + $command = "Invoke-SqlCmd @spaltArguments $additionalArguments" + $hasVerbose = $additionalArguments -match '(^|\s)-verbose($|\s)' + if ($hasVerbose) + { + $ErrorActionPreference = 'Continue' + $errors = @() + $rawOutput = (Invoke-Expression "$command" -ErrorVariable errors 4>&1) | Out-String + $trimmedOutput = $rawOutput.TrimEnd() + $trimmedOutput -split "`r?`n" | Where-Object { $_.Trim() -ne "" } | ForEach-Object { Write-Output $_ } + if ($errors.Count -gt 0) + { + throw "Command failed with errors: $($errors -join "`n")" + } + $ErrorActionPreference = 'Stop' + } + else + { + Invoke-Expression "$command" + } } # End of Try Finally