Decided to do a quick-hack/fast-publish on this one as I have had a bit less time to create a nice clean production-ready version as of yet… and people has been asking about how far off the article is.
What this script does is to send a text message using a GSM/GPRS modem connected to a local (or LAN-connected with local drivers) serial port using Powershell.
Disclaimer!
This script “works” but is not fit for production. See it as an example of the general concept to evolve and adapt into something worthy of production use.
What’s missing in the latest iteration is:
- A working Event-Handler to deal with asynchronous call-backs.
- Support for AT+MSGW (write to modem memory)
- Reusing messages in modem memory for multiple recipients.
- Various error- and exeption-handlers.
- Actually verifying that the modem is AT-capable.
- Querying the system for available modems and their ports.
The Script
So, a short note before digging into the script. Prerequisites for this script is that you have identified which COM-port to use and it’s supported baud-rates and whether it supports DTR or not. If you do not know what the hell I am talking about, you could probably have it work with my preconfigured settings anyway. If you are unsure about if your modem supports AT commands you could open a serial connection to the modem using Hyperterminal or PuTTY and run AT+CMGF=1. If supported, the return should be OK. If it is not supported (you get ERROR instead) you would have to use PDU-mode which require a bit of hex-encoding of your messages. This is nothing I have had to do yet and will not be including in this script. Maybe in the future. Maybe.
So, looking a some powershelling then. First thing would be to connect to the modem.
# Create your instance of the SerialPort Class |
With the connection established, you the set to modem in AT-mode and start sending the message to the modem.
# Tell the modem you want to use AT-mode |
As you may notice, the message is only stored in the modems memory until you send a Ctrl+z which will end the message and send it. It is possible to add more text to the message before sending it if you would like. Personally, I prefer to store the message into a regular powershell variable and pass that one to the script. The SerialPort library is sort of clever and a local phone number will probably work. For safety, I always use international number with country code as it will work every time.
Only thing left now is to close the serial port connection and end the script. Like this.
$serialPort.Close() |
The Copy/Paste part
Here’s the entire script with some added control-features and check to avoid trying to send text messages with no serial connection.
### DISCLAIMER ### |
Enjoy!