diff --git a/src/MonitoringTools/Public/Get-SystemHealth.ps1 b/src/MonitoringTools/Public/Get-SystemHealth.ps1 index 9d709ec3..43f6549c 100644 --- a/src/MonitoringTools/Public/Get-SystemHealth.ps1 +++ b/src/MonitoringTools/Public/Get-SystemHealth.ps1 @@ -6,9 +6,11 @@ function Get-SystemHealth { Returns CPU usage, disk free space and recent event log summary. The combined snapshot is also written to the structured log. #> - [CmdletBinding(SupportsShouldProcess=$true)] +[CmdletBinding(SupportsShouldProcess=$true)] param() + if (-not $PSCmdlet.ShouldProcess('system health')) { return } + $computer = if ($env:COMPUTERNAME) { $env:COMPUTERNAME } else { $env:HOSTNAME } $timestamp = (Get-Date).ToString('o') diff --git a/tests/MonitoringTools/Get-SystemHealth.Tests.ps1 b/tests/MonitoringTools/Get-SystemHealth.Tests.ps1 index 6bb91075..ec9ff9de 100644 --- a/tests/MonitoringTools/Get-SystemHealth.Tests.ps1 +++ b/tests/MonitoringTools/Get-SystemHealth.Tests.ps1 @@ -19,4 +19,16 @@ Describe 'Get-SystemHealth function' { $result.DiskInfo | Should -Be $disk $result.EventLogSummary | Should -Be $events } + + Safe-It 'does not query health when -WhatIf specified' { + Mock Get-CPUUsage {} -ModuleName MonitoringTools + Mock Get-DiskSpaceInfo {} -ModuleName MonitoringTools + Mock Get-EventLogSummary {} -ModuleName MonitoringTools + + Get-SystemHealth -WhatIf + + Assert-MockCalled Get-CPUUsage -Times 0 -ModuleName MonitoringTools + Assert-MockCalled Get-DiskSpaceInfo -Times 0 -ModuleName MonitoringTools + Assert-MockCalled Get-EventLogSummary -Times 0 -ModuleName MonitoringTools + } }