Run a PowerShell script on received SMS messages

The Scripting Connector in Diafaan SMS Server can be used to run a PowerShell script for every received SMS message. The process requires the latest version of Diafaan SMS Server and the use of the .NET 4.5 platform. There are a few extra steps required to get the C# script to work.


The first step is to add the library file ‘System.Management.Automation.dll’ to the installation folder of Diafaan SMS Server. This can be done with the following PowerShell command (this requires administrator privileges):

Copy ([PSObject].Assembly.Location) "C:\Program Files\Diafaan SMS Server"

If your system is not set up to be able to execute PowerShell scripts, or if the execution of scripts is restricted, you may have to use the following command to enable running local unsigned scripts:

Set-ExecutionPolicy RemoteSigned

Powershell commands to prepare for the C# script in the Scripting Connector

Powershell commands to prepare for the C# script in the Scripting Connector

Create the PowerShell script file

We found that the best way to run a PowerShell script from Diafaan SMS Server is to wrap the script in a function with the sender number and the message text as parameters of the function:

function wrapper($number, $message) {
  <# Do the work here, e.g.:
  $number | Out-File 'C:\temp\messages.txt' -Append;
  $message | Out-File 'C:\temp\messages.txt' -Append;

This script was saved in the file ‘C:\temp\script.ps1’.

Create a new Scripting Connector

After creating a new C# Scripting Connector you can change the script with the ‘Edit script’ link in the settings of the Scripting Connector. To enable the PowerShell functionality the top of the script must be appended with a number of lines so that it looks like this:

// Diafaan SMS Server Scripting Connector skeleton script
// AddReference System.dll
// AddReference System.Core.dll
// AddReference System.Management.Automation.dll
using System;
using System.Collections;
using System.Management.Automation;
using System.Collections.ObjectModel;

To call the PowerShell script with the from address and the message text for every received message the OnMessageReceived function in the C# script must be replaced with the following code:

private void OnMessageReceived(string fromAddress, string toAddress, string message, string messageType,
                               string messageId, string smsc, string pdu, string gateway,
                               DateTime sendTime, DateTime receiveTime)
  try {
    using (PowerShell PowerShellInstance = PowerShell.Create()) {
      PowerShellInstance.AddCommand(@"c:\\temp\\script.ps1", false);
      PowerShellInstance.AddCommand("wrapper").AddParameter("message", message).AddParameter("number", fromAddress);
      if (PowerShellInstance.Streams.Error.Count > 0) {  			 
        PostEventLog(PowerShellInstance.Streams.Error[0].Exception.Message, "", EventLog.Information);	
  catch (Exception e) {
    PostEventLog(e.Message, e.ToString(), EventLog.Error);