Skip to content

Commit

Permalink
Enable/Disable-DbaTraceFlag, support -WhatIf
Browse files Browse the repository at this point in the history
  • Loading branch information
niphlod committed Feb 17, 2025
1 parent 65c66e9 commit 5c1b4ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
31 changes: 19 additions & 12 deletions public/Disable-DbaTraceFlag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
}
}
Expand Down
31 changes: 19 additions & 12 deletions public/Enable-DbaTraceFlag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Disable-DbaTraceFlag.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Describe "Disable-DbaTraceFlag" -Tag "UnitTests" {
"SqlInstance",
"SqlCredential",
"TraceFlag",
"EnableException"
"EnableException",
"Confirm",
"WhatIf"
)
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Enable-DbaTraceFlag.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Describe "Enable-DbaTraceFlag" -Tag "UnitTests" {
"SqlInstance",
"SqlCredential",
"TraceFlag",
"EnableException"
"EnableException",
"Confirm",
"WhatIf"
)
}

Expand Down

0 comments on commit 5c1b4ee

Please sign in to comment.