Skip to content

Commit

Permalink
Updated parameter names and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan committed Jun 20, 2023
1 parent 9fe4f6d commit 6e38efc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
79 changes: 58 additions & 21 deletions public/Get-DbaMemoryUsage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,41 @@ function Get-DbaMemoryUsage {
For MFA support, please use Connect-DbaInstance.
.PARAMETER MemoryCounterRegex
Regular expression that is applied to the paths of the counters returned for the counter list set '*sql*:Memory Manager*' to display relevant Memory Manager counters.
Default: '(Total Server Memory |Target Server Memory |Connection Memory |Lock Memory |SQL Cache Memory |Optimizer Memory |Granted Workspace Memory |Cursor memory usage|Maximum Workspace)'
The default works for English language systems and has to be adapted for other languages.
.PARAMETER PlanCounterRegex
Regular expression that is applied to the paths of the counters returned for the counter list set '*sql*:Plan Cache*' to display relevant Plan Cache counters.
Default: '(cache pages|procedure plan|ad hoc sql plan|prepared SQL Plan)'
The default works for English language systems and has to be adapted for other languages.
.PARAMETER BufferCounterRegex
Regular expression that is applied to the paths of the counters returned for the counter list set '*Buffer Manager*' to display relevant Buffer Manager counters.
Default: '(Free pages|Reserved pages|Stolen pages|Total pages|Database pages|target pages|extension .* pages)'
The default works for English language systems and has to be adapted for other languages.
.PARAMETER SSASCounterRegex
Regular expression that is applied to the paths of the counters returned for the counter list set 'MSAS*:Memory' to display relevant SSAS counters.
Default: '(\\memory )'
The default works for English language systems and has to be adapted for other languages.
.PARAMETER SSISCounterRegex
Regular expression that is applied to the paths of the counters returned for the counter list set '*SSIS*' to display relevant SSIS counters.
Default: '(memory)'
The default works for English language systems and has to be adapted for other languages.
.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.
Expand Down Expand Up @@ -60,25 +95,27 @@ function Get-DbaMemoryUsage {
[Alias("Host", "cn", "Server")]
[DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
[PSCredential]$Credential,
[string]$Memcounters = '(Total Server Memory |Target Server Memory |Connection Memory |Lock Memory |SQL Cache Memory |Optimizer Memory |Granted Workspace Memory |Cursor memory usage|Maximum Workspace)',
[string]$Plancounters = '(cache pages|procedure plan|ad hoc sql plan|prepared SQL Plan)',
[string]$BufManpagecounters = '(Free pages|Reserved pages|Stolen pages|Total pages|Database pages|target pages|extension .* pages)',
[string]$SSAScounters = '(\\memory )',
[string]$SSIScounters = '(memory)',
[string]$MemoryCounterRegex = '(Total Server Memory |Target Server Memory |Connection Memory |Lock Memory |SQL Cache Memory |Optimizer Memory |Granted Workspace Memory |Cursor memory usage|Maximum Workspace)',
[string]$PlanCounterRegex = '(cache pages|procedure plan|ad hoc sql plan|prepared SQL Plan)',
[string]$BufferCounterRegex = '(Free pages|Reserved pages|Stolen pages|Total pages|Database pages|target pages|extension .* pages)',
[string]$SSASCounterRegex = '(\\memory )',
[string]$SSISCounterRegex = '(memory)',
[switch]$EnableException
)
begin {
$scriptBlock = {
param ($Memcounters,
$Plancounters,
$BufManpagecounters,
$SSAScounters,
$SSIScounters)
param (
$MemoryCounterRegex,
$PlanCounterRegex,
$BufferCounterRegex,
$SSASCounterRegex,
$SSISCounterRegex
)
<# DO NOT use Write-Message as this is inside of a script block #>
Write-Verbose -Message "Searching for Memory Manager Counters on $Computer"
try {
$availablecounters = (Get-Counter -ListSet '*sql*:Memory Manager*' -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availablecounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $Memcounters } | ForEach-Object {
$availableCounters = (Get-Counter -ListSet '*sql*:Memory Manager*' -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availableCounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $MemoryCounterRegex } | ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
Expand All @@ -97,8 +134,8 @@ function Get-DbaMemoryUsage {
<# DO NOT use Write-Message as this is inside of a script block #>
Write-Verbose -Message "Searching for Plan Cache Counters on $Computer"
try {
$availablecounters = (Get-Counter -ListSet '*sql*:Plan Cache*' -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availablecounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $Plancounters } | ForEach-Object {
$availableCounters = (Get-Counter -ListSet '*sql*:Plan Cache*' -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availableCounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $PlanCounterRegex } | ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
Expand All @@ -117,8 +154,8 @@ function Get-DbaMemoryUsage {
<# DO NOT use Write-Message as this is inside of a script block #>
Write-Verbose -Message "Searching for Buffer Manager Counters on $Computer"
try {
$availablecounters = (Get-Counter -ListSet "*Buffer Manager*" -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availablecounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $BufManpagecounters } | ForEach-Object {
$availableCounters = (Get-Counter -ListSet "*Buffer Manager*" -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availableCounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $BufferCounterRegex } | ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
Expand All @@ -137,8 +174,8 @@ function Get-DbaMemoryUsage {
<# DO NOT use Write-Message as this is inside of a script block #>
Write-Verbose -Message "Searching for SSAS Counters on $Computer"
try {
$availablecounters = (Get-Counter -ListSet "MSAS*:Memory" -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availablecounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $SSAScounters } | ForEach-Object {
$availableCounters = (Get-Counter -ListSet "MSAS*:Memory" -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availableCounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $SSASCounterRegex } | ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
Expand All @@ -157,8 +194,8 @@ function Get-DbaMemoryUsage {
<# DO NOT use Write-Message as this is inside of a script block #>
Write-Verbose -Message "Searching for SSIS Counters on $Computer"
try {
$availablecounters = (Get-Counter -ListSet "*SSIS*" -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availablecounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $SSIScounters } | ForEach-Object {
$availableCounters = (Get-Counter -ListSet "*SSIS*" -ErrorAction SilentlyContinue).paths
(Get-Counter -Counter $availableCounters -ErrorAction SilentlyContinue).countersamples | Where-Object { $_.Path -match $SSISCounterRegex } | ForEach-Object {
$instance = (($_.Path.split("\")[-2]).replace("mssql`$", "")).split(':')[0]
if ($instance -eq 'sqlserver') { $instance = 'mssqlserver' }
[PSCustomObject]@{
Expand All @@ -182,7 +219,7 @@ function Get-DbaMemoryUsage {
if ($reply.FullComputerName) {
$Computer = $reply.FullComputerName
try {
foreach ($result in (Invoke-Command2 -ComputerName $Computer -Credential $Credential -ScriptBlock $scriptBlock -argumentlist $Memcounters, $Plancounters, $BufManpagecounters, $SSAScounters, $SSIScounters)) {
foreach ($result in (Invoke-Command2 -ComputerName $Computer -Credential $Credential -ScriptBlock $scriptBlock -argumentlist $MemoryCounterRegex, $PlanCounterRegex, $BufferCounterRegex, $SSASCounterRegex, $SSISCounterRegex)) {
[PSCustomObject]@{
ComputerName = $result.ComputerName
SqlInstance = $result.SqlInstance
Expand Down
2 changes: 1 addition & 1 deletion tests/Get-DbaMemoryUsage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'Credential', 'EnableException'
[object[]]$knownParameters = 'ComputerName', 'Credential', 'MemoryCounterRegex', 'PlanCounterRegex', 'BufferCounterRegex', 'SSASCounterRegex', 'SSISCounterRegex', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand Down

0 comments on commit 6e38efc

Please sign in to comment.