Scott Prince (Customer) asked a question.

PowerShell Disk Capacity Active Monitoring

Hello, my organization is looking to setup an active monitor that monitors available disk capacity, basically we'd like to monitor all logical disks and have a monitor alert if and when the disk drops below 10% of available capacity.

 

I have setup a basic PS script to check all disks and failures within the $context.SetResult however the result, when testing in WUG always returns as 0 (no disks are below capacity), if I was to comment out the $context.SetResult and run the script on a known server where a disk has less than 10% capacity then the script returns the correct outputs into $result = 1 & $ResultText = "Disks with less than 10% free space: $($failedDisks -join ', ')"

 

Would anyone be able to advise me exactly where I'm going wrong when I copy the script into WUG.

  1. $disks = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 } # Retrieves logical disks excluding CD-ROM, network drives, etc.
  2. $result = 0
  3. $failedDisks = @()
  4.  
  5. ForEach ($disk in $disks) {
  6. $freeSpacePercent = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 2)
  7. if ($freeSpacePercent -lt 10) {
  8. $result = 1
  9. $failedDisks += $disk.DeviceID
  10. }
  11. if ($result -eq 1) {
  12. $resultText = "Disks with less than 10% free space: $($failedDisks -join ', ')"
  13. }
  14. else {
  15. $resultText = "All disks have more than 10% free space"
  16. }
  17. }
  18. # Always set the active monitor result: 0 = success, 1 = failure
  19. $Context.SetResult($result, $resultText);

 


Jason Alberino likes this.