Skip to content

Commit a074e4e

Browse files
authored
Enable/Disable-DbaTraceFlag, support -WhatIf (#9588)
1 parent 10bc2d3 commit a074e4e

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

public/Disable-DbaTraceFlag.ps1

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ function Disable-DbaTraceFlag {
1818
For MFA support, please use Connect-DbaInstance.
1919
2020
.PARAMETER TraceFlag
21-
Trace flag number to enable globally
21+
Trace flag number to disable globally
22+
23+
.PARAMETER WhatIf
24+
Shows what would happen if the command were to run. No actions are actually performed.
25+
26+
.PARAMETER Confirm
27+
Prompts you for confirmation before executing any changing operations within the command.
2228
2329
.PARAMETER EnableException
2430
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 {
4248
Disable the globally running trace flag 3226 on SQL Server instance sql2016
4349
4450
#>
45-
[CmdletBinding()]
51+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
4652
param (
4753
[parameter(Mandatory, ValueFromPipeline)]
4854
[DbaInstanceParameter[]]$SqlInstance,
@@ -80,18 +86,19 @@ function Disable-DbaTraceFlag {
8086
Write-Message -Level Warning -Message "Trace Flag $tf is not currently running on $instance"
8187
continue
8288
}
83-
84-
try {
85-
$query = "DBCC TRACEOFF ($tf, -1)"
86-
$server.Query($query)
87-
} catch {
88-
$TraceFlagInfo.Status = "Failed"
89-
$TraceFlagInfo.Notes = $_.Exception.Message
89+
if ($Pscmdlet.ShouldProcess($instance, "Disabling flag '$tf'")) {
90+
try {
91+
$query = "DBCC TRACEOFF ($tf, -1)"
92+
$server.Query($query)
93+
} catch {
94+
$TraceFlagInfo.Status = "Failed"
95+
$TraceFlagInfo.Notes = $_.Exception.Message
96+
$TraceFlagInfo
97+
Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
98+
}
99+
$TraceFlagInfo.Status = "Successful"
90100
$TraceFlagInfo
91-
Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
92101
}
93-
$TraceFlagInfo.Status = "Successful"
94-
$TraceFlagInfo
95102
}
96103
}
97104
}

public/Enable-DbaTraceFlag.ps1

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ function Enable-DbaTraceFlag {
2020
.PARAMETER TraceFlag
2121
Trace flag number(s) to enable globally
2222
23+
.PARAMETER WhatIf
24+
Shows what would happen if the command were to run. No actions are actually performed.
25+
26+
.PARAMETER Confirm
27+
Prompts you for confirmation before executing any changing operations within the command.
28+
2329
.PARAMETER EnableException
2430
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
2531
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 {
4652
4753
Enable multiple trace flags on SQL Server instance sql2016
4854
#>
49-
[CmdletBinding()]
55+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
5056
param (
5157
[parameter(Mandatory, ValueFromPipeline)]
5258
[DbaInstanceParameter[]]$SqlInstance,
@@ -84,19 +90,20 @@ function Enable-DbaTraceFlag {
8490
Write-Message -Level Warning -Message "The Trace flag [$tf] is already running globally."
8591
continue
8692
}
87-
88-
try {
89-
$query = "DBCC TRACEON ($tf, -1)"
90-
$server.Query($query)
91-
$server.Refresh()
92-
} catch {
93-
$TraceFlagInfo.Status = "Failed"
94-
$TraceFlagInfo.Notes = $_.Exception.Message
93+
if ($Pscmdlet.ShouldProcess($instance, "Enabling flag '$tf'")) {
94+
try {
95+
$query = "DBCC TRACEON ($tf, -1)"
96+
$server.Query($query)
97+
$server.Refresh()
98+
} catch {
99+
$TraceFlagInfo.Status = "Failed"
100+
$TraceFlagInfo.Notes = $_.Exception.Message
101+
$TraceFlagInfo
102+
Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
103+
}
104+
$TraceFlagInfo.Status = "Successful"
95105
$TraceFlagInfo
96-
Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
97106
}
98-
$TraceFlagInfo.Status = "Successful"
99-
$TraceFlagInfo
100107
}
101108
}
102109
}

tests/Disable-DbaTraceFlag.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Describe "Disable-DbaTraceFlag" -Tag "UnitTests" {
1313
"SqlInstance",
1414
"SqlCredential",
1515
"TraceFlag",
16-
"EnableException"
16+
"EnableException",
17+
"Confirm",
18+
"WhatIf"
1719
)
1820
}
1921

tests/Enable-DbaTraceFlag.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Describe "Enable-DbaTraceFlag" -Tag "UnitTests" {
1313
"SqlInstance",
1414
"SqlCredential",
1515
"TraceFlag",
16-
"EnableException"
16+
"EnableException",
17+
"Confirm",
18+
"WhatIf"
1719
)
1820
}
1921

0 commit comments

Comments
 (0)