-
Notifications
You must be signed in to change notification settings - Fork 1
/
cSQLServerTraceFlagHelper.psm1
168 lines (139 loc) · 5.22 KB
/
cSQLServerTraceFlagHelper.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
function Get-SQLTraceFlag
{
[CmdletBinding()]
[OutputType([System.String[]])]
Param
(
[Parameter(Mandatory = $true,
Position = 1)]
[System.String]
$SQLInstanceName
)
Initialize-SQLSMOAssembly
$SQLManagement = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $env:COMPUTERNAME
$WMIService = $SQLManagement.Services | Where-Object -FilterScript { $PSItem.Name -eq $SQLInstanceName }
$ParameterList = $WMIService.StartupParameters.Split(';')
$ParameterList | Where-Object -FilterScript { $PSItem -like '-T*' } | ForEach-Object {
$PSItem.TrimStart('-')
}
}
function Set-SQLTraceFlag
{
[CmdletBinding()]
Param
(
[Parameter(Position = 1)]
[System.String]
$SQLInstanceName = 'MSSQLServer',
[Parameter(Mandatory = $true,
Position = 2)]
[System.String[]]
$TraceFlag,
[switch]
$RestartSQLService
)
Initialize-SQLSMOAssembly
$sqlManagement = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $env:COMPUTERNAME
$wmiService = $sqlManagement.Services | Where-Object -FilterScript { $PSItem.Name -eq $SQLInstanceName }
# Add '-' dash to flag
$traceFlagList = $TraceFlag | ForEach-Object {
"-$PSItem"
}
# Extract flags from startup parameters
[System.Collections.ArrayList]$parameterList = $wmiService.StartupParameters.Split(';')
# Removing extra flags
foreach ($parameter in $wmiService.StartupParameters.Split(';')) {
if ($parameter -like '-T*' -and $parameter -notin $traceFlagList) {
$parameterList.Remove($parameter) | Out-Null
}
}
# Add missing flags
foreach ($Flag in $traceFlagList) {
if ($Flag -notin $parameterList) {
$parameterList.Add($Flag) | Out-Null
}
}
# Merge flags back into startup parameters
$wmiService.StartupParameters = $parameterList -join ';'
$wmiService.Alter()
if ($RestartSQLService)
{
$wmiService.Stop()
Start-Sleep -Seconds 10
$wmiService.Start()
($sqlManagement.Services | Where-Object -FilterScript { $PSItem.Name -eq 'SQLSERVERAGENT' }).Start()
}
}
function Remove-SQLTraceFlag
{
[CmdletBinding()]
Param
(
[Parameter(Position = 1)]
[System.String]
$SQLInstanceName = 'MSSQLServer',
[Parameter(Mandatory = $true,
Position = 2)]
[System.String[]]
$TraceFlag,
[switch]
$RestartSQLService
)
Initialize-SQLSMOAssembly
$sqlManagement = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $env:COMPUTERNAME
$wmiService = $sqlManagement.Services | Where-Object -FilterScript { $PSItem.Name -eq $SQLInstanceName }
# Add '-' dash to flag
$traceFlagList = $TraceFlag | ForEach-Object {
"-$PSItem"
}
# Extract flags from startup parameters
[System.Collections.ArrayList]$parameterList = $wmiService.StartupParameters.Split(';')
# Add missing flags
foreach ($Flag in $traceFlagList) {
if ($Flag -in $parameterList) {
$parameterList.Remove($Flag) | Out-Null
}
}
# Merge flags back into startup parameters
$wmiService.StartupParameters = $parameterList -join ';'
$wmiService.Alter()
if ($RestartSQLService)
{
$wmiService.Stop()
Start-Sleep -Seconds 10
$wmiService.Start()
($sqlManagement.Services | Where-Object -FilterScript { $PSItem.Name -eq 'SQLSERVERAGENT' }).Start()
}
}
function Initialize-SQLSMOAssembly {
# Source: https://blog.netnerds.net/2016/12/loading-smo-in-your-sql-server-centric-powershell-modules/
$smoversions = "14.0.0.0", "13.0.0.0", "12.0.0.0", "11.0.0.0", "10.0.0.0", "9.0.242.0", "9.0.0.0"
foreach ($smoversion in $smoversions)
{
try
{
Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=$smoversion, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop
$smoadded = $true
}
catch
{
$smoadded = $false
}
if ($smoadded -eq $true) { break }
}
if ($smoadded -eq $false) { throw "Can't load SMO assemblies. You must have SQL Server Management Studio installed to proceed." }
$assemblies = "Management.Common", "Dmf", "Instapi", "SqlWmiManagement", "ConnectionInfo", "SmoExtended", "SqlTDiagM", "Management.Utility",
"SString", "Management.RegisteredServers", "Management.Sdk.Sfc", "SqlEnum", "RegSvrEnum", "WmiEnum", "ServiceBrokerEnum", "Management.XEvent",
"ConnectionInfoExtended", "Management.Collector", "Management.CollectorEnum", "Management.Dac", "Management.DacEnum", "Management.IntegrationServices"
foreach ($assembly in $assemblies)
{
try
{
Add-Type -AssemblyName "Microsoft.SqlServer.$assembly, Version=$smoversion, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop
}
catch
{
# Don't care
}
}
}