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 1/2] 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 From 16ba4fc8cb83daff1480b2902bbd5850ae7c779e Mon Sep 17 00:00:00 2001 From: "Abhishek Raj (LTIMindtree Limited)" Date: Wed, 15 Oct 2025 11:42:07 +0530 Subject: [PATCH 2/2] Implemented Catch logic for failing task if there is error in inline script --- .../SqlQueryOnTargetMachines.ps1 | 31 ++++++++++--------- .../TaskModuleSqlUtility.psd1 | 2 +- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 index d8709708c..121317593 100644 --- a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 +++ b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 @@ -87,27 +87,28 @@ function Invoke-SqlQueryDeployment $additionalArguments = EscapeSpecialChars $additionalArguments - Write-Verbose "Invoke-SqlCmd arguments : $commandToLog $additionalArguments" + $commandToRun = $commandToLog + " " + $additionalArguments $command = "Invoke-SqlCmd @spaltArguments $additionalArguments" - $hasVerbose = $additionalArguments -match '(^|\s)-verbose($|\s)' - if ($hasVerbose) - { - $ErrorActionPreference = 'Continue' + + Write-Host "##[command] $commandToRun" + + if ($additionalArguments.ToLower().Contains("-verbose")) { $errors = @() - $rawOutput = (Invoke-Expression "$command" -ErrorVariable errors 4>&1) | Out-String - $trimmedOutput = $rawOutput.TrimEnd() + + $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")" + + if ($errors.Count -gt 0) { + throw } - $ErrorActionPreference = 'Stop' } - else - { - Invoke-Expression "$command" + else { + Invoke-Expression $command } - + } + Catch { + Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow } # End of Try Finally { diff --git a/TaskModules/powershell/TaskModuleSqlUtility/TaskModuleSqlUtility.psd1 b/TaskModules/powershell/TaskModuleSqlUtility/TaskModuleSqlUtility.psd1 index b84cf50b1..520d1a730 100644 --- a/TaskModules/powershell/TaskModuleSqlUtility/TaskModuleSqlUtility.psd1 +++ b/TaskModules/powershell/TaskModuleSqlUtility/TaskModuleSqlUtility.psd1 @@ -1,6 +1,6 @@ @{ RootModule = 'TaskModuleSqlUtility.psm1' - ModuleVersion = '0.1.3' + ModuleVersion = '0.1.6' GUID = 'd997c6dd-33ad-481c-859b-01120229b91f' Author = 'Microsoft' CompanyName = 'Microsoft'