-
Notifications
You must be signed in to change notification settings - Fork 1
/
cSQLServerTraceFlag.psm1
138 lines (115 loc) · 3.96 KB
/
cSQLServerTraceFlag.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
# Load helper module
Import-Module -Name "$PSScriptRoot\cSQLServerTraceFlagHelper.psm1" -Verbose:$false -ErrorAction Stop
enum Ensure
{
Absent
Present
}
[DscResource()]
class cSQLServerTraceFlag
{
[DscProperty(Mandatory)]
[Ensure]
$Ensure
[DscProperty(Key)]
[System.String]
$SQLInstanceName
[DscProperty(Mandatory)]
[System.String[]]
$TraceFlag
[DscProperty()]
[System.Boolean]
$RestartSQLService
[void] Set()
{
if ($this.Ensure -eq [Ensure]::Present)
{
# Create mandatory parameters
$addSQLTraceFlagParameters = @{
SQLInstanceName = $this.SQLInstanceName
TraceFlag = $this.TraceFlag
RestartSQLService = $this.RestartSQLService
}
Set-SQLTraceFlag @addSQLTraceFlagParameters
}
if ($this.Ensure -eq [Ensure]::Absent)
{
# Create mandatory parameters
$addSQLTraceFlagParameters = @{
SQLInstanceName = $this.SQLInstanceName
TraceFlag = $this.TraceFlag
RestartSQLService = $this.RestartSQLService
}
Remove-SQLTraceFlag @addSQLTraceFlagParameters
}
}
[bool] Test()
{
$currentStatus = $this.Get()
$status = $true
if ($this.Ensure -eq [Ensure]::Present)
{
if ($currentStatus.Ensure -eq [Ensure]::Absent)
{
Write-Verbose -Message ("'TraceFlag' does not match desired state. Current value: '{0}'. Desired Value: '{1}'." -f ($currentStatus.TraceFlag -join ','),($this.TraceFlag -join ','))
$status = $false
}
}
if ($this.Ensure -eq [Ensure]::Absent)
{
if ($currentStatus.Ensure -eq [Ensure]::Present)
{
# Retrieve desired flag list
$desiredFlagList = [System.Collections.ArrayList]$currentStatus.TraceFlag
$this.TraceFlag | Where-Object -FilterScript { $PSItem -in $this.TraceFlag } | ForEach-Object {
$desiredFlagList.Remove($PSItem)
}
Write-Verbose -Message ("'TraceFlag' does not match desired state. Current value: '{0}'. Desired Value: '{1}'." -f ($currentStatus.TraceFlag -join ','),($desiredFlagList -join ','))
$status = $false
}
}
return $status
}
[cSQLServerTraceFlag] Get()
{
$status = [cSQLServerTraceFlag]::new()
# Create mandatory parameters
$getSQLTraceFlagParameters = @{
SQLInstanceName = $this.SQLInstanceName
}
$flagList = Get-SQLTraceFlag @getSQLTraceFlagParameters
# Locate flags that should be in the flag list
if ($this.Ensure -eq [Ensure]::Present)
{
$status.Ensure = [Ensure]::Present
if ($null -ne $flagList)
{
if ($null -ne (Compare-Object -ReferenceObject $flagList -DifferenceObject $this.TraceFlag))
{
$status.Ensure = [Ensure]::Absent
}
}
else
{
$status.Ensure = [Ensure]::Absent
}
}
# Locate flags that should not be in the flag list
if ($this.Ensure -eq [Ensure]::Absent)
{
$status.Ensure = [Ensure]::Absent
if ($null -ne $flagList)
{
foreach ($flag in $this.TraceFlag)
{
if ($flag -in $flagList)
{
$status.Ensure = [Ensure]::Present
}
}
}
}
$status.TraceFlag = $flagList
return $status
}
}