Monitoring with PowerShell: Monitoring NAS devices

This was actually requested a couple of times but I’ve always seem to hold off on it, mostly because we don’t use that many NAS devices anymore. I did decide to make it a bit more universal than just NAS devices really – It’s about monitoring any device that has SSH access and of which you know the commands that you need. I’ve just used NAS’s as an example in this case.

We’re using SSH because most NAS devices support it – e.g. QNAP/Synology/etc. I like this method more than SNMP monitoring because I strongly believe more in agent based monitoring than SNMP probing.

So lets get started.

The Script

The script itself is very straight-forward; we download the SSH-Posh module by Carlos Perez and then connect to the NAS using SSH, we execute our command defined in $Command. In the case of most NAS devices you can use the command “cat /proc/mdstat”. After executing this command we compare if it contains any words we don’t like, such as fail, rebuild, recovery, etc. 🙂

$Command = 'cat /proc/mdstat'
$NotHealthyStates = "*recovery*", "*failed*", "*failure*", "*offline*", "*rebuilding*"
$Creds = Get-Credential #Replace this with your RMMs method to handle credentials.

If (Get-Module -ListAvailable -Name "Posh-SSH") { Import-module "Posh-SSH" } Else { install-module "Posh-SSH" -Force; import-module "Posh-SSH" }

try {
    $null = New-SSHSession -ComputerName $DeviceIP -Credential $Creds -AcceptKey -force
    $SSHOutput = (Invoke-SSHCommand -Index 0 -Command $command).output
    $null = Get-SSHSession | Remove-SSHSession
catch {
    write-host "Unhealthy - Could not connect to SSH."
if (!$SSHOutput) { write-host "Unhealthy - No SSH output found" ; break }

$HealthState = foreach ($State in $NotHealthyStates) {
    if ($SSHOutput -like $State) {
        "Unhealthy - $State found."

if (!$HealthState) { write-host "Healthy - No issues found." } else { write-host $HealthState }

And that’s it! as always, Happy PowerShelling. 🙂

1 Comment

  1. Tim Beard December 3, 2020 at 11:17 pm

    I am looking for someone that can write me some scripts to be run in the Task section on the Solarwinds RMM Dashboard. Since Solarwinds Support doesn’t write scripts, one of their support techs, Marc Tanguay, suggested I contact you to see if you are interested in some contract work.

    Here is an example of a script I need: I have Backup and Recovery (Managed Online Backup) running daily on a server. I also am using the Solarwinds Recovery Console running continuous restore daily to a USB External hard drive on a workstation on the same LAN. Unfortunately, continuous restore doesn’t show up on the dashboard so I have no way of knowing it’s status without actually logging onto the workstation to look at it. Very disruptive to the user. I want a script to run on the Task tab on my dashboard to let me know it’s status. From my understanding, this can be accomplished with a script, checking event codes.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.