functions/Test-SqlTempDBConfiguration.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
250
251
252
253
254
255
256
Function Test-SqlTempDbConfiguration
{
<#
.SYNOPSIS
Evaluates tempdb against several rules to match best practices.
 
.DESCRIPTION
Evaluates tempdb aganst 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, as 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. Only results that don't match best practices will be displayed. To display all results,
use the -Detailed switch.
 
.PARAMETER SqlServer
The SQL Server instance.You must have sysadmin access and server version must be SQL Server version 2000 or higher.
 
.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
 
$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.
 
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user.
 
.PARAMETER Detailed
Switch that allows you to display all test results.
 
.NOTES
Original 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
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Test-SqlTempDbConfiguration
 
.EXAMPLE
Test-SqlTempDbConfiguration -SqlServer localhost
 
Checks tempdb on the localhost machine.
 
.EXAMPLE
Test-SqlTempDbConfiguration -SqlServer localhost -Detailed
 
Checks tempdb on the localhost machine. All rest results are shown.
 
#>


[CmdletBinding()]
Param (
[parameter(Mandatory = $true)]
[Alias("ServerInstance", "SqlInstance")]
[object]$SqlServer,
[object]$SqlCredential,
[Switch]$Detailed
)

BEGIN
{
$result = @()
Write-Verbose "Connecting to $SqlServer"
$server = Connect-SqlServer $SqlServer -SqlCredential $SqlCredential
}

PROCESS
{
#test for TF 1118
if ($server.VersionMajor -ge 13)
{
$notes = 'SQL 2016 has this functionality enabled by default'
# DBA May have changed setting. May need to check.
$value = [PSCustomObject]@{
Rule = 'TF 1118 Enabled'
Recommended = $true
CurrentSetting = $true
}
}
else
{
$sql = "dbcc traceon (3604);dbcc tracestatus (-1)"
$tfcheck = $server.Databases['tempdb'].ExecuteWithResults($sql).Tables[0].TraceFlag
$notes = 'KB328551 describes how TF 1118 can benefit performance.'

if (($tfcheck -join ',').Contains('1118'))
{

$value = [PSCustomObject]@{
Rule = 'TF 1118 Enabled'
Recommended = $true
CurrentSetting = $true
}
}
else
{
$value = [PSCustomObject]@{
Rule = 'TF 1118 Enabled'
Recommended = $true
CurrentSetting = $false
}
}
}

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

$value | Add-Member -MemberType NoteProperty -Name IsBestpractice -Value $isBestPractice
$value | Add-Member -MemberType NoteProperty -Name Notes -Value $notes
$result += $value
Write-Verbose "TF 1118 evaluated"

#get files and log files
$datafiles = $server.Databases['tempdb'].ExecuteWithResults("SELECT physical_name as FileName, max_size as MaxSize, CASE WHEN is_percent_growth = 1 THEN 'Percent' ELSE 'KB' END as GrowthType from sys.database_files WHERE type_desc = 'ROWS'").Tables[0]
$logfiles =  $server.Databases['tempdb'].ExecuteWithResults("SELECT physical_name as FileName, max_size as MaxSize, CASE WHEN is_percent_growth = 1 THEN 'Percent' ELSE 'KB' END as GrowthType from sys.database_files WHERE type_desc = 'LOG'").Tables[0]

Write-Verbose "TempDB file objects gathered"

$cores = $server.Processors

if ($cores -gt 8) { $cores = 8 }

$value = [PSCustomObject]@{
Rule = 'File Count'
Recommended = $cores
CurrentSetting = $datafiles.Rows.Count
}

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

$value | Add-Member -MemberType NoteProperty -Name IsBestpractice -Value $isBestPractice
$value | Add-Member -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-Verbose "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]@{
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
}

$value | Add-Member -MemberType NoteProperty -Name IsBestpractice -Value $isBestPractice
$value | Add-Member -MemberType NoteProperty -Name Notes -Value 'Set grow with explicit values, not by percent.'
$result += $value

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

$cdata = ($datafiles | Where-Object { $_.FileName -like 'C:*' } | Measure-Object).Count + ($logfiles | Where-Object { $_.FileName -like 'C:*' } | Measure-Object).Count
if ($cdata -gt 0) { $cdata = $true } else { $cdata = $false }

$value = [PSCustomObject]@{
Rule = 'File Location'
Recommended = $false
CurrentSetting = $cdata
}

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

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

Write-Verbose "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]@{
Rule = 'File MaxSize Set'
Recommended = $false
CurrentSetting = $growthlimits
}

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

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

Write-Verbose "MaxSize values evaluated"
}

END
{
if($Detailed)
{
return $result
}
else
{
$failed = $result | Where-Object { $_.isBestPractice -eq $false }
if ($null -eq $failed)
{
Write-Output "All tests passed! tempdb is properly optimized."
}
else
{
return $failed
}
}
}
}