Please forgive me if this information is found elsewhere, I couldn't find anything close enough. I apologize in advanced if this is the wrong location to ask for help.
I'm writing a custom PowerShell monitor to use for monitoring disk free space. I'm in the process of converting our current monitoring solution from a different product and attempting to get SAM to monitor the same thresholds, for consistency. I'm very experienced in PowerShell (though maybe not an expert) and I'm happy with the script; however, I cannot get the script to return any results in SAM when I'm testing it during the New Application Template process. If I run the script on the polling engine or the target node in PowerShell ISE directly, it returns correctly, however, running it in SAM against the same node returns zero results.
Here is the script:
#Requires -Version 2.0
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -ErrorAction SilentlyContinue | Out-Null
$output = Get-WmiObject -Class win32_logicalDisk -ComputerName ${Node.Dns} |
Where-Object { $_.DriveType -Eq '3'} |
Where-Object { ($_.Name -Eq 'C:' -And $_.Freespace -Le '300000000') -Or ($_.Name -Ne 'C:' -And $_.Freespace -Le '1000000000') } |
Select-Object -Property Name,
@{Expression={"{0:N1}" -f $($_.Freespace / $_.Size * 100)};Name='%Free'},
@{Expression={"{0:N0}" -f $($_.Freespace / 1MB) -Replace ','};Name='FreespaceMB'},
@{Expression={"{0:N0}" -f $($_.Size / 1MB) -Replace ','};Name='SizeMB'} |
Format-List | Out-String
$oFormat= ($output -Split "`n" | Where-Object { $_.Trim() -Ne '' } | Foreach-Object {
"<p style=`"font-family:Consolas;font-size:12px`">$_</p>"
}) -Join "`n" | Out-String
If ($output) {
Write-Host "Statistic.DisksLow : $(($output | Measure-Object).Count)"
Write-Host "Message.DisksLow: $oFormat"
} Else {
Write-Host "Statistic.DisksLow : 0"
Write-Host "Message.DisksLow : No logical disks' free space are below the critical threshold."
}
As a proof-of-concept, I've tried to get it to display anything and have narrowed it down to the Get-WmiObject command itself. I have commented out everything except the initial Get-WmiObject command (no pipes), which should result in showing all attached drives, and the $output variable is empty. I have also commented out the $oFormat variable, but it doesn't matter because the $output variable is still empty no matter what I do (unless I run it directly from the polling engine or target node). I've even tried explicitly defining the $output variable before the $oFormat variable and was able to get a result, so I'm confident the Get-WmiObject command just isn't showing anything and I can't figure out why it wouldn't, when it does on the server itself.
We're running SAM version 6.4.0. Our polling engine is Windows Server 2008 R2. The target node I'm testing is also Windows Server 2008 R2. The user account configured in SAM for WMI is a local admin on both servers.
This one is baffling me... Any insight is appreciated.
Refer to the attached screenshots.
-Nick