functions/Test-DbaTempDbConfiguration.ps1

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
function Test-DbaTempDbConfiguration {
    <#
        .SYNOPSIS
            Evaluates tempdb against several rules to match best practices.
 
        .DESCRIPTION
            Evaluates tempdb against a set of rules to match best practices. The rules are:
 
            * TF 1118 enabled - Is Trace Flag 1118 enabled (See KB328551).
            * File Count - Does the count of data files in tempdb match the number of logical cores, up to 8?
            * File Growth - Are any files set to have percentage growth? Best practice is all files have an explicit growth value.
            * File Location - Is tempdb located on the C:\? Best practice says to locate it elsewhere.
            * File MaxSize Set (optional) - Do any files have a max size value? Max size could cause tempdb problems if it isn't allowed to grow.
 
            Other rules can be added at a future date.
 
        .PARAMETER SqlInstance
            The SQL Server Instance to connect to. SQL Server 2005 and higher are supported.
 
        .PARAMETER SqlCredential
            Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
 
        .PARAMETER Detailed
            Output all properties, will be depreciated in 1.0.0 release.
 
        .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.
            Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
        .NOTES
            Tags: tempdb, configuration
            Author: Michael Fal (@Mike_Fal), http://mikefal.net
            Based off of Amit Bannerjee's (@banerjeeamit) Get-TempDB function (https://github.com/amitmsft/SqlOnAzureVM/blob/master/Get-TempdbFiles.ps1)
 
            dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
            Copyright (C) 2016 Chrissy LeMaire
            License: MIT https://opensource.org/licenses/MIT
 
        .LINK
            https://dbatools.io/Test-DbaTempDbConfiguration
 
        .EXAMPLE
            Test-DbaTempDbConfiguration -SqlInstance localhost
 
            Checks tempdb on the localhost machine.
 
        .EXAMPLE
            Test-DbaTempDbConfiguration -SqlInstance localhost | Select-Object *
 
            Checks tempdb on the localhost machine. All rest results are shown.
    #>

    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstance[]]$SqlInstance,
        [PSCredential]$SqlCredential,
        [switch]$Detailed,
        [Alias('Silent')]
        [switch]$EnableException
    )
    begin {
        Test-DbaDeprecation -DeprecatedOn 1.0.0 -Parameter Detailed

        $result = @()
    }
    process {
        foreach ($instance in $SqlInstance) {
            Write-Message -Level Verbose -Message "Attempting to connect to $instance"
            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }

            #test for TF 1118
            if ($server.VersionMajor -ge 13) {
                $notes = 'SQL Server 2016 has this functionality enabled by default'
                # DBA May have changed setting. May need to check.
                $value = [PSCustomObject]@{
                    ComputerName   = $server.NetName
                    InstanceName   = $server.ServiceName
                    SqlInstance    = $server.DomainInstanceName
                    Rule           = 'TF 1118 Enabled'
                    Recommended    = $true
                    CurrentSetting = $true
                }
            }
            else {
                $sql = "DBCC TRACEON (3604);DBCC TRACESTATUS(-1)"
                $tfCheck = $server.Databases['tempdb'].Query($sql)
                $notes = 'KB328551 describes how TF 1118 can benefit performance.'

                $value = [PSCustomObject]@{
                    ComputerName   = $server.NetName
                    InstanceName   = $server.ServiceName
                    SqlInstance    = $server.DomainInstanceName
                    Rule           = 'TF 1118 Enabled'
                    Recommended    = $true
                    CurrentSetting = ($tfCheck.TraceFlag -join ',').Contains('1118')
                }
            }

            if ($value.Recommended -ne $value.CurrentSetting -and $null -ne $value.Recommended) {
                $isBestPractice = $false
            }
            else {
                $isBestPractice = $true
            }

            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name IsBestPractice -Value $isBestPractice
            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name Notes -Value $notes
            $result += $value
            Write-Message -Level Verbose -Message "TF 1118 evaluated"

            #get files and log files
            $tempdbFiles = Get-DbaDatabaseFile -SqlInstance $server -Database tempdb
            [array]$dataFiles = $tempdbFiles | Where-Object Type -ne 1
            $logFiles = $tempdbFiles | Where-Object Type -eq 1
            Write-Message -Level Verbose -Message "TempDB file objects gathered"

            $value = [PSCustomObject]@{
                ComputerName   = $server.NetName
                InstanceName   = $server.ServiceName
                SqlInstance    = $server.DomainInstanceName
                Rule           = 'File Count'
                Recommended    = [Math]::Min(8, $server.Processors)
                CurrentSetting = $dataFiles.Count
            }

            if ($value.Recommended -ne $value.CurrentSetting -and $null -ne $value.Recommended) {
                $isBestPractice = $false
            }
            else {
                $isBestPractice = $true
            }

            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name IsBestPractice -Value $isBestPractice
            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name Notes -Value 'Microsoft recommends that the number of tempdb data files is equal to the number of logical cores up to 8.'
            $result += $value

            Write-Message -Level Verbose -Message "File counts evaluated."

            #test file growth
            $percData = $dataFiles | Where-Object GrowthType -ne 'KB' | Measure-Object
            $percLog = $logFiles  | Where-Object GrowthType -ne 'KB' | Measure-Object

            $totalCount = $percData.Count + $percLog.Count
            if ($totalCount -gt 0) {
                $totalCount = $true
            }
            else {
                $totalCount = $false
            }

            $value = [PSCustomObject]@{
                ComputerName   = $server.NetName
                InstanceName   = $server.ServiceName
                SqlInstance    = $server.DomainInstanceName
                Rule           = 'File Growth in Percent'
                Recommended    = $false
                CurrentSetting = $totalCount
            }

            if ($value.Recommended -ne $value.CurrentSetting -and $null -ne $value.Recommended) {
                $isBestPractice = $false
            }
            else {
                $isBestPractice = $true
            }

            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name IsBestPractice -Value $isBestPractice
            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name Notes -Value 'Set file growth to explicit values, not by percent.'
            $result += $value

            Write-Message -Level Verbose -Message "File growth settings evaluated."
            #test file Location

            $cdata = ($dataFiles | Where-Object PhysicalName -like 'C:*' | Measure-Object).Count + ($logFiles | Where-Object PhysicalName -like 'C:*' | Measure-Object).Count
            if ($cdata -gt 0) {
                $cdata = $true
            }
            else {
                $cdata = $false
            }

            $value = [PSCustomObject]@{
                ComputerName   = $server.NetName
                InstanceName   = $server.ServiceName
                SqlInstance    = $server.DomainInstanceName
                Rule           = 'File Location'
                Recommended    = $false
                CurrentSetting = $cdata
            }

            if ($value.Recommended -ne $value.CurrentSetting -and $null -ne $value.Recommended) {
                $isBestPractice = $false
            }
            else {
                $isBestPractice = $true
            }

            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name IsBestPractice -Value $isBestPractice
            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name Notes -Value "Do not place your tempdb files on C:\."
            $result += $value

            Write-Message -Level Verbose -Message "File locations evaluated."

            #Test growth limits
            $growthLimits = ($dataFiles | Where-Object MaxSize -gt 0 | Measure-Object).Count + ($logFiles | Where-Object MaxSize -gt 0 | Measure-Object).Count
            if ($growthLimits -gt 0) {
                $growthLimits = $true
            }
            else {
                $growthLimits = $false
            }

            $value = [PSCustomObject]@{
                ComputerName   = $server.NetName
                InstanceName   = $server.ServiceName
                SqlInstance    = $server.DomainInstanceName
                Rule           = 'File MaxSize Set'
                Recommended    = $false
                CurrentSetting = $growthLimits
            }

            if ($value.Recommended -ne $value.CurrentSetting -and $null -ne $value.Recommended) {
                $isBestPractice = $false
            }
            else {
                $isBestPractice = $true
            }

            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name IsBestPractice -Value $isBestPractice
            Add-Member -Force -InputObject $value -MemberType NoteProperty -Name Notes -Value "Consider setting your tempdb files to unlimited growth."
            $result += $value

            Write-Message -Level Verbose -Message "MaxSize values evaluated."

            Select-DefaultView -InputObject $result -Property ComputerName, InstanceName, SqlInstance, Rule, Recommended, IsBestPractice
        }
    }
    end {
        Test-DbaDeprecation -DeprecatedOn "1.0.0" -EnableException:$false -Alias Test-SqlTempDbConfiguration
    }
}