From a074e4eec8503cb25b7497f716b6e57b0d34f0d2 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 24 Feb 2025 15:11:49 +0100 Subject: [PATCH] Enable/Disable-DbaTraceFlag, support -WhatIf (#9588) --- public/Disable-DbaTraceFlag.ps1 | 31 +++++++++++++++++----------- public/Enable-DbaTraceFlag.ps1 | 31 +++++++++++++++++----------- tests/Disable-DbaTraceFlag.Tests.ps1 | 4 +++- tests/Enable-DbaTraceFlag.Tests.ps1 | 4 +++- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/public/Disable-DbaTraceFlag.ps1 b/public/Disable-DbaTraceFlag.ps1 index 2425494711..1a353ecd73 100644 --- a/public/Disable-DbaTraceFlag.ps1 +++ b/public/Disable-DbaTraceFlag.ps1 @@ -18,7 +18,13 @@ function Disable-DbaTraceFlag { For MFA support, please use Connect-DbaInstance. .PARAMETER TraceFlag - Trace flag number to enable globally + Trace flag number to disable globally + + .PARAMETER WhatIf + Shows what would happen if the command were to run. No actions are actually performed. + + .PARAMETER Confirm + Prompts you for confirmation before executing any changing operations within the command. .PARAMETER EnableException By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. @@ -42,7 +48,7 @@ function Disable-DbaTraceFlag { Disable the globally running trace flag 3226 on SQL Server instance sql2016 #> - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] param ( [parameter(Mandatory, ValueFromPipeline)] [DbaInstanceParameter[]]$SqlInstance, @@ -80,18 +86,19 @@ function Disable-DbaTraceFlag { Write-Message -Level Warning -Message "Trace Flag $tf is not currently running on $instance" continue } - - try { - $query = "DBCC TRACEOFF ($tf, -1)" - $server.Query($query) - } catch { - $TraceFlagInfo.Status = "Failed" - $TraceFlagInfo.Notes = $_.Exception.Message + if ($Pscmdlet.ShouldProcess($instance, "Disabling flag '$tf'")) { + try { + $query = "DBCC TRACEOFF ($tf, -1)" + $server.Query($query) + } catch { + $TraceFlagInfo.Status = "Failed" + $TraceFlagInfo.Notes = $_.Exception.Message + $TraceFlagInfo + Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue + } + $TraceFlagInfo.Status = "Successful" $TraceFlagInfo - Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue } - $TraceFlagInfo.Status = "Successful" - $TraceFlagInfo } } } diff --git a/public/Enable-DbaTraceFlag.ps1 b/public/Enable-DbaTraceFlag.ps1 index a69b78ae1a..acc1e31431 100644 --- a/public/Enable-DbaTraceFlag.ps1 +++ b/public/Enable-DbaTraceFlag.ps1 @@ -20,6 +20,12 @@ function Enable-DbaTraceFlag { .PARAMETER TraceFlag Trace flag number(s) to enable globally + .PARAMETER WhatIf + Shows what would happen if the command were to run. No actions are actually performed. + + .PARAMETER Confirm + Prompts you for confirmation before executing any changing operations within the command. + .PARAMETER EnableException By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. @@ -46,7 +52,7 @@ function Enable-DbaTraceFlag { Enable multiple trace flags on SQL Server instance sql2016 #> - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] param ( [parameter(Mandatory, ValueFromPipeline)] [DbaInstanceParameter[]]$SqlInstance, @@ -84,19 +90,20 @@ function Enable-DbaTraceFlag { Write-Message -Level Warning -Message "The Trace flag [$tf] is already running globally." continue } - - try { - $query = "DBCC TRACEON ($tf, -1)" - $server.Query($query) - $server.Refresh() - } catch { - $TraceFlagInfo.Status = "Failed" - $TraceFlagInfo.Notes = $_.Exception.Message + if ($Pscmdlet.ShouldProcess($instance, "Enabling flag '$tf'")) { + try { + $query = "DBCC TRACEON ($tf, -1)" + $server.Query($query) + $server.Refresh() + } catch { + $TraceFlagInfo.Status = "Failed" + $TraceFlagInfo.Notes = $_.Exception.Message + $TraceFlagInfo + Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue + } + $TraceFlagInfo.Status = "Successful" $TraceFlagInfo - Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue } - $TraceFlagInfo.Status = "Successful" - $TraceFlagInfo } } } diff --git a/tests/Disable-DbaTraceFlag.Tests.ps1 b/tests/Disable-DbaTraceFlag.Tests.ps1 index 51f3201927..824c8d2608 100644 --- a/tests/Disable-DbaTraceFlag.Tests.ps1 +++ b/tests/Disable-DbaTraceFlag.Tests.ps1 @@ -13,7 +13,9 @@ Describe "Disable-DbaTraceFlag" -Tag "UnitTests" { "SqlInstance", "SqlCredential", "TraceFlag", - "EnableException" + "EnableException", + "Confirm", + "WhatIf" ) } diff --git a/tests/Enable-DbaTraceFlag.Tests.ps1 b/tests/Enable-DbaTraceFlag.Tests.ps1 index 07e95d32e7..ce469b3db8 100644 --- a/tests/Enable-DbaTraceFlag.Tests.ps1 +++ b/tests/Enable-DbaTraceFlag.Tests.ps1 @@ -13,7 +13,9 @@ Describe "Enable-DbaTraceFlag" -Tag "UnitTests" { "SqlInstance", "SqlCredential", "TraceFlag", - "EnableException" + "EnableException", + "Confirm", + "WhatIf" ) }