This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

SAM - PowerShell Monitor for Disk Free Space not working

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

attachments.zip
  • You can do a try-catch on your WMI call and have success or failure output the message being returned to at least get pointed towards what's failing.

  • That was what I needed. Turns out the script is running under the computer object of the Orion server, not the SAM credentials. Any reason why that is? Once I added the Try/Catch I received an "Access is denied". Then I added the computer object of the Orion server to the local admin group on the target node, the script provided the output I expected. Shouldn't the script execute as the credentials I choose in the SAM template configuration? Those credentials have admin access to the target node and therefor would work.

  • It should be using the SAM account you specify on the template.

    Double check your template and node settings. Typically my application monitors are set to inherit the credentials defined on the node, but not always. You may also be seeing an issue with PSRemoting depending on if your execution mode is set to local or remote host.

    You can confirm the user account by doing another test run:
    $currentuser = whoami

    write-host "Message.User: $currentuser"

  • Well i can narrow it down to your environment or poller, as your script executes without any problems in my environment.

    pastedImage_0.png

  • My credentials were set to be inherited already, but thanks for the suggestion. I was searching around for other things it could be and ran into a tidbit someone used that wasn't intended on fixing my problem (or there's) but it interested me so I tried it and it ended up working.

    I modified:

    Get-WmiObject -Class win32_logicalDisk -ComputerName ${Node.Dns}

    To be:

    Get-WmiObject -Class win32_logicalDisk -ComputerName ${Node.Dns} -Credential ${CREDENTIAL}

    And that appears to have resolved my issue. Thanks everyone for your time.

  • Thanks for the reply. This was the behavior I was experiencing as well, and my problem. It always showed that, even when there were results running the script directly on the target node. The $output variable always ended up being blank and therefor falling into the Else statement at the end.

    Following styler's suggestion, I added a Try/Catch statement and realized I was getting an "Accessed is denied" error. The SAM module was running from the poller's local SYSTEM account. When I added the computer object of the poller's machine to the local Administrator's group of the target node, it executed successfully. Not wanting to add the poller's computer object to all servers in my environment, I removed it and started searching for credentials-related troubleshooting in SAM. I came across something someone was using when they had an issue. I don't know if they ever fixed their issue, but I noticed they had "-Credential ${CREDENTIAL}" included in their PowerShell script. I added it to mine and it resolved my credentials issue.