From 4f83b141f379c73018603aad760a2d8ccba17e6f Mon Sep 17 00:00:00 2001 From: "Abhishek Raj (LTIMindtree Limited)" Date: Thu, 17 Jul 2025 17:10:47 +0530 Subject: [PATCH 1/5] Enhanced the functionality of SqlDacPacDeploymentonOnMachineGroupV0 --- .../TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 | 9 ++++++++- .../TaskModuleSqlUtility/TaskModuleSqlUtility.psd1 | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 index 2a58aa82f..f17f35c0b 100644 --- a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 +++ b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 @@ -88,7 +88,14 @@ function Invoke-SqlQueryDeployment $additionalArguments = EscapeSpecialChars $additionalArguments Write-Verbose "Invoke-SqlCmd arguments : $commandToLog $additionalArguments" - Invoke-Expression "Invoke-SqlCmd @spaltArguments $additionalArguments" + if ($additionalArguments.ToLower().Contains("-verbose")) { + $rawOutput = (Invoke-Expression "Invoke-SqlCmd @spaltArguments $additionalArguments" 4>&1) | Out-String + $trimmedOutput = $rawOutput.TrimEnd() + $trimmedOutput -split "`r?`n" | ForEach-Object { Write-Output $_ } + } + else { + Invoke-Expression "Invoke-SqlCmd @spaltArguments $additionalArguments" + } } # 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' From d2cd8fcf5a0689a16467562a607777cc3c0fdaec Mon Sep 17 00:00:00 2001 From: "Abhishek Raj (LTIMindtree Limited)" Date: Wed, 15 Oct 2025 13:14:28 +0530 Subject: [PATCH 2/5] Implemented Catch logic for failing task if there is error in inline script --- .../SqlQueryOnTargetMachines.ps1 | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 index f17f35c0b..121317593 100644 --- a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 +++ b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 @@ -87,16 +87,28 @@ function Invoke-SqlQueryDeployment $additionalArguments = EscapeSpecialChars $additionalArguments - Write-Verbose "Invoke-SqlCmd arguments : $commandToLog $additionalArguments" + $commandToRun = $commandToLog + " " + $additionalArguments + $command = "Invoke-SqlCmd @spaltArguments $additionalArguments" + + Write-Host "##[command] $commandToRun" + if ($additionalArguments.ToLower().Contains("-verbose")) { - $rawOutput = (Invoke-Expression "Invoke-SqlCmd @spaltArguments $additionalArguments" 4>&1) | Out-String + $errors = @() + + $rawOutput = (Invoke-Expression $command -ErrorVariable errors 4>&1 | Out-String) $trimmedOutput = $rawOutput.TrimEnd() - $trimmedOutput -split "`r?`n" | ForEach-Object { Write-Output $_ } + $trimmedOutput -split "`r?`n" | Where-Object { $_.Trim() -ne "" } | ForEach-Object { Write-Output $_ } + + if ($errors.Count -gt 0) { + throw + } } else { - Invoke-Expression "Invoke-SqlCmd @spaltArguments $additionalArguments" + Invoke-Expression $command } - + } + Catch { + Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow } # End of Try Finally { From 8c4a006069d1b023911f55ad5e0f606d36ccbf54 Mon Sep 17 00:00:00 2001 From: "Abhishek Raj (LTIMindtree Limited)" Date: Thu, 23 Oct 2025 17:22:00 +0530 Subject: [PATCH 3/5] Fix output buffering - implement live streaming display --- .../TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 index 121317593..539f2304f 100644 --- a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 +++ b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 @@ -95,9 +95,9 @@ function Invoke-SqlQueryDeployment if ($additionalArguments.ToLower().Contains("-verbose")) { $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 $_ } + Invoke-Expression $command -ErrorVariable errors 4>&1 | ForEach-Object { + Write-Host $_ + } if ($errors.Count -gt 0) { throw From 8b6b43ccbb1a22526969813c33ea5c4728b97e46 Mon Sep 17 00:00:00 2001 From: "Abhishek Raj (LTIMindtree Limited)" Date: Wed, 29 Oct 2025 10:49:33 +0530 Subject: [PATCH 4/5] Adding entire change under enableVerboseLogging Feature Flags --- .../SqlQueryOnTargetMachines.ps1 | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 index 539f2304f..8e7895042 100644 --- a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 +++ b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 @@ -1,4 +1,7 @@ -# Function to import SqlPS module & avoid directory switch +$featureFlags = @{ + enableVerboseLogging = [System.Convert]::ToBoolean($env:ENABLE_VERBOSE_LOGGING) +} +# Function to import SqlPS module & avoid directory switch function Import-SqlPs { push-location Import-Module SqlPS -ErrorAction 'SilentlyContinue' | out-null @@ -86,29 +89,38 @@ function Invoke-SqlQueryDeployment } $additionalArguments = EscapeSpecialChars $additionalArguments + if ($featureFlags.enableVerboseLogging) { + $commandToRun = $commandToLog + " " + $additionalArguments + $command = "Invoke-SqlCmd @spaltArguments $additionalArguments" + Write-Host "##[command] $commandToRun" - $commandToRun = $commandToLog + " " + $additionalArguments - $command = "Invoke-SqlCmd @spaltArguments $additionalArguments" - - Write-Host "##[command] $commandToRun" + if ($additionalArguments.ToLower().Contains("-verbose")) { + $errors = @() - if ($additionalArguments.ToLower().Contains("-verbose")) { - $errors = @() + Invoke-Expression $command -ErrorVariable errors 4>&1 | ForEach-Object { + Write-Host $_ + } - Invoke-Expression $command -ErrorVariable errors 4>&1 | ForEach-Object { - Write-Host $_ + if ($errors.Count -gt 0) { + throw + } } - - if ($errors.Count -gt 0) { - throw + else{ + Invoke-Expression $command } } else { - Invoke-Expression $command + Write-Verbose "Invoke-SqlCmd arguments : $commandToLog $additionalArguments" + Invoke-Expression "Invoke-SqlCmd @spaltArguments $additionalArguments" } } Catch { - Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow + if ($featureFlags.enableVerboseLogging) { + Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow + } + else{ + Write-VstsSetResult -Result 'Failed' -Message "Error detected without verboseFF logging" -DoNotThrow + } } # End of Try Finally { From adf4f832467d1eeffda24e4905a441944e2fae38 Mon Sep 17 00:00:00 2001 From: "Abhishek Raj (LTIMindtree Limited)" Date: Tue, 4 Nov 2025 15:05:45 +0530 Subject: [PATCH 5/5] Catching exceptions without FF enabled. --- .../TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 index 8e7895042..d4eec0381 100644 --- a/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 +++ b/TaskModules/powershell/TaskModuleSqlUtility/SqlQueryOnTargetMachines.ps1 @@ -105,7 +105,7 @@ function Invoke-SqlQueryDeployment throw } } - else{ + else { Invoke-Expression $command } } @@ -116,10 +116,10 @@ function Invoke-SqlQueryDeployment } Catch { if ($featureFlags.enableVerboseLogging) { - Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow + Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow } - else{ - Write-VstsSetResult -Result 'Failed' -Message "Error detected without verboseFF logging" -DoNotThrow + else { + throw $_.Exception } } # End of Try Finally