Sheridan Wendt
  • Home
  • Technology
  • Business
  • Music
  • Adventures
  • Resume
  • Contact

Technology

Send SMS Notifications with PowerShell

3/30/2018

Comments

 
Ever had an outage while you were away from the office and you didn't (or couldn't) check your email from your mobile phone? What if your environment could notify administrators via SMS (text) messages about things going on in the environment? It can! For free! If you can't afford to purchase a more robust monitoring and notification system, SMS notifications can be automated internally for free. In this article we'll go over how to send SMS messages using an SMTP server and PowerShell.
Picture
​Click below to view the full .ps1 on github
SMSNotifications.ps1
First things first, the purpose of these SMS notifications will determine most of the variables shown. Since this is just for demonstrative purposes, we'll have some fun with it! 

The Varibles
  • The $Count variable is used to count how many messages have been sent. It starts at 0 and increases by one each time a message is sent.
  • The $MaxCount variable is the number of times that an SMS message will be sent.
  • The $Messages array is a list of messages, in case sending the same message each time isn't effective. This script will work with one message or multiple messages. 
  • The $ToPhone variable is the recipient's phone number
  • The $ToCarrier is the SMS gateway of the carrier of the recipient. This is required and must be known ahead of time.  Wikipedia has a pretty comprehensive list of SMS and MMS gateway domains. The difference between SMS and MMS is that an MMS gateway can generally accept pictures, video, and other multimedia.
  • The $Subject is not required but shows up in bold in most text messages
  • The $SMTPServer variable is the IP Address or FQDN of a simple mail transfer protocol server. For most organizations this is your primary email server and may require authentication.
  • The $Timer variable is the length of time, in seconds, between each message

Here are the variables from our example script:
"​$Count = 0
$MaxCount = 10
$Messages = "The Force is with you", "You are one with the Force", "You are the last Jedi", "Do or Do Not. There is no try."
$ToPhone = "1428571428"
$ToCarrier = "mms.att.net"
$Subject = "Yoda Says:"
$SMTPServer = 9.9.9.9"
$Timer = 300
The Actions
Next up is where all the magic happens. You may recognize this as a classic Do / While statement. That is because we do an action (like send a text) while the count is less than 10, at least in this case. The nice part about this particular script is that each line has text before the action that tells the user what the script is doing. These are optional and can be removed.

Let's go through each line and then we'll put it all together. The first step inside the Do statement is to declare the $Count variable. Then the count is displayed to the user. 
​Do {
    $Count 
    "Count at $Count"
Next we generate a random 12 character string to be used in the from address. This step is not necessary but ensures that the recipient cannot block the messages, because they will always be coming from a different address. If you'd prefer not to generate a random address so that the texts can be viewed in one ongoing thread on a mobile device, simply define the $Sender as some other fixed string.
"Generating random number"
 $Sender = -join ((65..90) + (97..122) | Get-Random -Count 12 | % {[char]$_})
Next up one of the messages in the array called $Message is selected at random:
"Selecting a random message from the Messages variable"
$Message = $Messages[(Get-Random -Maximum ([array]$Messages).count)]
Next the actual message is sent based off of the previously defined variables
"Sending message"
Send-MailMessage -To "$ToPhone@$ToCarrier" -From "$Sender@domain.com" -Subject "$Subject" -Body "$Message" -SmtpServer $SMTPServer
Next a string is shown to the user that the message was sent and the timer starts, using the $Timer variable to show the duration:
"Message Sent"
Start-Sleep -s $Timer
    }
Lastly, the while statement keeps count of how many times we've made it back to the while statement. As long as the messages are being sent, it's also counting how many of those have been sent and terminates the script after the count reaches the $MaxCount. Ten in this script.
​While ($Count++ -le $MaxCount)
Finally , here's what we get when we put it all together:
# ********************************************************************************
#
# Script Name: SMSNotifications.ps1
# Version: 1.0
# Author: Sheridan Wendt
# Date: 9/26/2017
# Applies to: SMS Messages via Email
#
# Description: This script assigns a various list of customizable messages to the 
# variable $Messages, generates a random email address (so these notifications
# cannot be blocked by the recipient), and sends the message via email to a phone
# carrier's email:sms gateway. A 5 minute break is taken and then the next text
# is sent until 10 (number assigned to $MaxCount) notifications have been sent.
# or the process is cancelled.
#
# Resource: https://en.wikipedia.org/wiki/SMS_gateway#Spreadsheet-to-SMS_gateway
#
# ********************************************************************************

$Count = 0
$MaxCount = 10
$Messages = "The Force is with you", "You are one with the Force", "You are the last Jedi", "Do or Do Not. There is no try."
$ToPhone = "1428571428"
$ToCarrier = "mms.att.net"
$Subject = "Yoda Says:"
$SMTPServer = "9.9.9.9"
$Timer = 300

Do {
    $Count 
    "Count at $Count"
    "Generating random number"
    $Sender = -join ((65..90) + (97..122) | Get-Random -Count 12 | % {[char]$_})
    "Selecting a random message from the Messages variable"
    $Message = $Messages[(Get-Random -Maximum ([array]$Messages).count)]
    "Sending message"
    Send-MailMessage -To "$ToPhone@$ToCarrier" -From "$Sender@domain.com" -Subject "$Subject" -Body "$Message" -SmtpServer $SMTPServer
    "Message Sent"
    Start-Sleep -s $Timer
    }
While ($Count++ -le $MaxCount)
Picture
Comments

    Repositories

    PowerShell
    SQL

    Author

    Sheridan's interests are in technology, business, music, and adventures

    View my profile on LinkedIn

    RSS Feed

    Categories

    All
    Alerts
    Azure
    Business Intelligence
    Data Visualization
    Notifications
    Photo Frame
    PowerShell
    Raspberry Pi
    Scripting
    SMS
    SQL
    Technology
    Virtualization
    VMWare

    Business

    Archives

    June 2019
    May 2019
    September 2018
    May 2018
    April 2018
    March 2018
    February 2018
    December 2017
    September 2017
    July 2003

Powered by Create your own unique website with customizable templates.
  • Home
  • Technology
  • Business
  • Music
  • Adventures
  • Resume
  • Contact