From dc9f092c34e7ec443a8bb5987a5b61e161fc3cee Mon Sep 17 00:00:00 2001 From: "Abhishek Raj (LTIMindtree Limited)" Date: Mon, 28 Jul 2025 16:59:41 +0530 Subject: [PATCH] Adding verbose functionality in the given task --- .../SqlQueryOnTargetMachines.ps1 | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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