Skip to content

Instantly share code, notes, and snippets.

@cmaggiulli
Created August 3, 2017 15:25
Show Gist options
  • Save cmaggiulli/175c9cebe7fb8900493c81f15593ebfe to your computer and use it in GitHub Desktop.
Save cmaggiulli/175c9cebe7fb8900493c81f15593ebfe to your computer and use it in GitHub Desktop.
This powershell script runs hourly via task scheduler and checks the number of MuliTech modems currently running against the amount that was running the last hour. If a diff exists send email to recipients. I wrote this script to monitor our outgoing fax server since RabbitMQ would not reflect a backup of faxes after handing off the message
function Send-ComPort-Alert
{
param([bool]$Asc, [string]$Modems)
# Sender and Receiver
$From = "sender@example.com"
$To = "you@example.com", "me@example.com"
# Server and Port
$Server = "smtp.server.com"
$Port = "587"
# Credentials
$Pwd = "Ex@mple1" | Convertto-SecureString -AsPlainText -Force
$Creds = New-Object System.Management.Automation.Pscredential -Argumentlist $From, $Pwd
# Content
$Dir = ({'down'},{'up'})[!$Asc]
$Subject = "WARNING: MultiTech modem went $Dir"
$Body = "A MultiTech modem went $Dir. Currently running $Modems"
# Send Email
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $Server -port $Port -UseSsl -Credential $Creds
}
# Get the last or initial number of MultiTech modems
$ComPortsCtPrev = (Get-ChildItem Env:COMPORTSCT).Value
# Get current multitech modems
$ComPorts = Get-WmiObject Win32_PnPSignedDriver | Where-Object {$_.Manufacturer -eq "MultiTech Systems"} | Select-Object FriendlyName
# Check for difference between current and previous
$ComPortStr = $ComPorts | Out-String
$ComPortsCt = $ComPorts.Count
$IsDiff = $ComPortsCtPrev -ne $ComPortsCt
# If diff exists send email
if($IsDiff)
{
Send-ComPort-Alert -Asc ($ComPortsCtPrev -lt $ComPortsCt) -Modems $ComPortStr
}
# Update environment variable to be used as the next previous value
setx COMPORTSCT $ComPortsCt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment