Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable/Disable-DbaTraceFlag, support -WhatIf #9588

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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