functions/Get-DbaMemoryUsage.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
Function Get-DbaMemoryUsage
{
<#
.SYNOPSIS
Get amount of memory in use by *all* SQL Server components and instances
 
.DESCRIPTION
Retrieves the amount of memory per performance counter. Default output includes columns Server, counter instance, counter, number of pages, memory in KB, memory in MB
SSAS and SSIS are included.
  
SSRS does not have memory counters, only memory shrinks and memory pressure state.
 
This function requires local admin role on the targeted computers.
 
.PARAMETER ComputerName
The Windows Server that you are connecting to. Note that this will return all instances, but Out-GridView makes it easy to filter to specific instances.
 
.PARAMETER Credential
Credential object used to connect to the SQL Server as a different user
 
.PARAMETER Simple
Shows concise information including Server name, Database name, and the date the last time backups were performed
 
.NOTES
Tags: Memory
Author: Klaas Vandenberghe ( @PowerDBAKlaas )
 
dbatools PowerShell module (https://dbatools.io)
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/.
 
SSIS Counters: https://msdn.microsoft.com/en-us/library/ms137622.aspx
 
.LINK
 https://dbatools.io/Get-DbaMemoryUsage
 
.EXAMPLE
Get-DbaMemoryUsage -ComputerName ServerA
 
Returns a custom object displaying Server, counter instance, counter, number of pages, memory in KB, memory in MB
 
.EXAMPLE
Get-DbaMemoryUsage -ComputerName ServerA\sql987 -Simple
 
Returns a custom object with Server, counter instance, counter, number of pages, memory in KB, memory in MB
 
.EXAMPLE
Get-DbaMemoryUsage -ComputerName ServerA\sql987 | Out-Gridview
 
Returns a gridview displaying Server, counter instance, counter, number of pages, memory in KB, memory in MB
 
#>

[CmdletBinding()]
Param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("Host", "cn", "Server")]
[string[]]$ComputerName,
[PsCredential]$Credential,
[switch]$Simple
)


BEGIN
{
    if ($Simple)
        {
        $Memcounters = '(Total Server Memory |Target Server Memory |Connection Memory |Lock Memory |SQL Cache Memory |Optimizer Memory |Granted Workspace Memory |Cursor memory usage|Maximum Workspace)'
        $Plancounters = 'total\)\\cache pages'
        $BufManpagecounters = 'Total pages'
        $SSAScounters = '(\\memory usage)'
        $SSIScounters = '(memory)'
        }
    else
        {
        $Memcounters = '(Total Server Memory |Target Server Memory |Connection Memory |Lock Memory |SQL Cache Memory |Optimizer Memory |Granted Workspace Memory |Cursor memory usage|Maximum Workspace)'
        $Plancounters = '(cache pages|procedure plan|ad hoc sql plan|prepared SQL Plan)'
        $BufManpagecounters = '(Free pages|Reserved pages|Stolen pages|Total pages|Database pages|target pages|extension .* pages)'
        $SSAScounters = '(\\memory )'
        $SSIScounters = '(memory)'
        }

    }

PROCESS
{
        foreach ($Computer in $ComputerName)
        {
            Write-Verbose "Connecting to $Computer"
$reply = Resolve-DbaNetworkName -ComputerName $Computer -Credential $Credential -ErrorAction SilentlyContinue
            if ( $reply.ComputerName )
            {
                $Computer = $reply.ComputerName
                Write-Verbose "$Computer is up and running"
                Write-Verbose "Searching for Memory Manager Counters on $Computer"
                try
                {
                $availablecounters = (Get-Counter -ComputerName $Computer -ListSet '*sql*:Memory Manager*' -ErrorAction SilentlyContinue ).paths
                (Get-Counter -ComputerName $Computer -Counter $availablecounters -ErrorAction SilentlyContinue ).countersamples | 
                    Where-Object {$_.Path -match $Memcounters} |
ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
                ComputerName = $Computer
                                SqlInstance = $instance
                CounterInstance = (($_.Path.split("\")[-2]).replace("mssql`$","")).split(':')[1]
                                Counter = $_.Path.split("\")[-1]
                Pages = $null
                MemKB = $_.cookedvalue
                MemMB = $_.cookedvalue / 1024
                                }
                            }
                }
                catch
                {
                Write-Verbose "No Memory Manager Counters on $Computer"
                }
                
                Write-Verbose "Searching for Plan Cache Counters on $Computer"
                try
                {
                $availablecounters = (Get-Counter -ComputerName $Computer -ListSet '*sql*:Plan Cache*' -ErrorAction SilentlyContinue ).paths
                (Get-Counter -ComputerName $Computer -Counter $availablecounters -ErrorAction SilentlyContinue ).countersamples |
                    Where-Object {$_.Path -match $Plancounters} |
ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
ComputerName = $Computer
SqlInstance = $instance
                CounterInstance = (($_.Path.split("\")[-2]).replace("mssql`$","")).split(':')[1]
                                Counter = $_.Path.split("\")[-1]
            Pages = $_.cookedvalue
            MemKB = $_.cookedvalue * 8192 / 1024
            MemMB = $_.cookedvalue * 8192 / 1048576
                                }
                            }
                }
                catch
                {
                Write-Verbose "No Plan Cache Counters on $Computer"
                }
                                
                Write-Verbose "Searching for Buffer Manager Counters on $Computer"
                try
                {
                $availablecounters = (Get-Counter -ComputerName $Computer -ListSet "*Buffer Manager*"  -ErrorAction SilentlyContinue ).paths
                (Get-Counter -ComputerName $Computer -Counter $availablecounters -ErrorAction SilentlyContinue ).countersamples |
                    Where-Object {$_.Path -match $BufManpagecounters} |
ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
ComputerName = $Computer
SqlInstance = $instance
                CounterInstance = (($_.Path.split("\")[-2]).replace("mssql`$","")).split(':')[1]
                                Counter = $_.Path.split("\")[-1]
            Pages = $_.cookedvalue
            MemKB = $_.cookedvalue * 8192 / 1024.0
            MemMB = $_.cookedvalue * 8192 /1048576.0
                                }
                            }
                }
                catch
                {
                Write-Verbose "No Buffer Manager Counters on $Computer"
                }
                                
                Write-Verbose "Searching for SSAS Counters on $Computer"
                try
                {
                $availablecounters = (Get-Counter -ComputerName $Computer -ListSet "MSAS*:Memory"  -ErrorAction SilentlyContinue ).paths
                (Get-Counter -ComputerName $Computer -Counter $availablecounters -ErrorAction SilentlyContinue ).countersamples |
                    Where-Object {$_.Path -match $SSAScounters} |
ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
ComputerName = $Computer
SqlInstance = $instance
                CounterInstance = (($_.Path.split("\")[-2]).replace("mssql`$","")).split(':')[1]
                                Counter = $_.Path.split("\")[-1]
            Pages = $null
            MemKB = $_.cookedvalue
            MemMB = $_.cookedvalue / 1024
                                }
                            }
                }
                catch
                {
                Write-Verbose "No SSAS Counters on $Computer"
                }
                                
                Write-Verbose "Searching for SSIS Counters on $Computer"
                try
                {
                $availablecounters = (Get-Counter -ComputerName $Computer -ListSet "*SSIS*"  -ErrorAction SilentlyContinue ).paths
                (Get-Counter -ComputerName $Computer -Counter $availablecounters -ErrorAction SilentlyContinue ).countersamples |
                    Where-Object {$_.Path -match $SSIScounters} |
ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
ComputerName = $Computer
SqlInstance = $instance
                CounterInstance = (($_.Path.split("\")[-2]).replace("mssql`$","")).split(':')[1]
                                Counter = $_.Path.split("\")[-1]
            Pages = $null
            MemKB = $_.cookedvalue / 1024
            MemMB = $_.cookedvalue / 1024 / 1024
                                }
                            }
                }
                catch
                {
                Write-Verbose "No SSIS Counters on $Computer"
                }
            }
else
{
Write-Warning "Can't connect to $Computer."
Continue
}
        }
    }
    END
    {}
}