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
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. If any of these rules don't match recommended values, a warning will be thrown.

.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.

.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.
    
#>

    
    [CmdletBinding(SupportsShouldProcess = $true)]
    Param (
        [parameter(Mandatory = $true)]
        [Alias("ServerInstance", "SqlInstance")]
        [object]$SqlServer,
        [object]$SqlCredential
    )
    
    BEGIN{
        [object[]]$return = @()
        Write-Verbose "Connecting to $SqlServer"
        $smosrv = Connect-SqlServer $SqlServer -SqlCredential $SqlCredential
    }
    

    PROCESS
    {
        #test for TF 1118
        if($smosrv.VersionMajor -ge 13){
            $value = [ordered]@{'Rule'='TF 1118 Enabled';'Recommended'=$true;'CurrentSetting'=$true;'Notes'='SQL 2016 has this functionality enabled by default'}
        } else {
            $sql="dbcc traceon (3604);dbcc tracestatus (-1)"
            $tfcheck=$smosrv.Databases['tempdb'].ExecuteWithResults($sql).Tables[0].TraceFlag
            if(($tfcheck -join ',').Contains('1118')){
                $value = [ordered]@{'Rule'='TF 1118 Enabled';'Recommended'=$true;'CurrentSetting'=$true;'Notes'='KB328551 describes how TF 1118 can benefit performance.'}
            } else {
                $value = [ordered]@{'Rule'='TF 1118 Enabled';'Recommended'=$true;'CurrentSetting'=$false;'Notes'='KB328551 describes how TF 1118 can benefit performance.'}
            }
        }
        Write-Verbose "TF 1118 evaluated"
        $return += New-Object psobject -Property $value

        #get files and log files
        $DataFiles = $smosrv.Databases['tempdb'].FileGroups[0].Files
        $LogFiles = $smosrv.Databases['tempdb'].LogFiles
        Write-Verbose "TempDB file objects gathered"

        #Test file count
        $cores = (Get-WmiObject -ComputerName $smosrv.ComputerNamePhysicalNetBIOS -Class Win32_Processor ).NumberOfLogicalProcessors
        if($cores -gt 8){$cores = 8}
        $filecount = $DataFiles.Count
        $value = [ordered]@{'Rule'='File Count';'Recommended'=$cores;'CurrentSetting'=$filecount;'Notes'="Microsoft recommends that the number of tempdb data files is equal to the number of logical cores up to 8."}
        
        Write-Verbose "File counts evaluated"
        $return += New-Object psobject -Property $value

        #test file growth
        $percgrowth = ($DataFiles | Where-Object {$_.GrowthType -ne 'KB'}).Count + ($LogFiles | Where-Object {$_.GrowthType -ne 'KB'}).Count
        $value = [ordered]@{'Rule'='File Growth';'Recommended'=0;'CurrentSetting'=$percgrowth;'Notes'="Set grow with explicit values, not by percent."}

        Write-Verbose "File growth settings evaluated"
        $return += New-Object psobject -Property $value

        #test file Location
        $locgrowth = ($DataFiles | Where-Object {$_.FileName -like 'C:*'}).Count + ($LogFiles | Where-Object {$_.FileName -like 'C:*'}).Count
        $value = [ordered]@{'Rule'='File Location';'Recommended'=0;'CurrentSetting'=$locgrowth;'Notes'="Do not place your tempdb files on C:\."}

        Write-Verbose "File locations evaluated"
        $return += New-Object psobject -Property $value
        
        #Test growth limits
        $growthlimits = ($DataFiles | Where-Object {$_.MaxSize -gt 0}).Count + ($LogFiles | Where-Object {$_.MaxSize -gt 0}).Count
        $value = [ordered]@{'Rule'='File MaxSize Set';'Recommended'= $null;'CurrentSetting'=$growthlimits;'Notes'="Consider setting your tempdb files to unlimited growth."}

        Write-Verbose "MaxSize values evaluated"
        $return += New-Object psobject -Property $value
    }
        
    END
    {
        $WarningCount = ($return | Where-Object {$_.Recommended -ne $_.CurrentSetting -and $_.Recommended -ne $null}).Count    
        if($WarningCount -gt 0){Write-Warning 'Some settings to not match recommended best practices.'}        
        return $return
                
    }
}