ActiveXperts SMS Component

Quicklinks

Powershell sample - Send SMS text through a COM/USB GSM modem

The ActiveXperts SMS Component is a software development kit (SDK) to enhance an application or script with SMS or Pager functionality. SMS messages can be sent/received using a GSM modem, an SMPP provider or an HTTP compliant SMSC.


Introduction

In this example we are going to create a Powershell Script to send SMS messages. This demo project will ask the user to give a phone number and a message body on the command prompt. A GSM modem is required for this demo.

Step 1: Download and install The ActiveXperts SMS Component

Download the ActiveXperts SMS Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new script

Create a new script using your favorite editor. You can simply use notepad. However, a Powershell editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced Powershell script to send SMS messages using the ActiveXperts SMS Component.

Step 3: Declare and create the ActiveXperts SMS Component objects

Create a new Powershell file called DEMO.PS1.

The following code will show you how to declare and create the GSM and SMS objects. We will use the 'objGsm' object to send the message itself. The 'objSmsMessage' object will be used to store information of the message and the 'objSmsConstants' object containes constant values releated to the SMS objects.

Insert the following line to declare and create the GSM object:

$objGsm          = new-object -comobject AxSms.Gsm
$objSmsMessage   = new-object -comobject AxSms.Message
$objSmsConstants = new-object -comobject AxSms.Constants

Step 4: Gather information

The following code will ask the user for the recipient telephone number and the content of the text message. This data will be stored in the 'objSmsMessage' object.

# Message: set all properties
$objSmsMessage.Clear()
$objSmsMessage.ToAddress       = ReadInput "Enter Recipient" "" $False
$objSmsMessage.Body            = ReadInput "Enter the message text:" "Hello, world!" $False
$objSmsMessage.ValidityPeriod  = 0  # Use GSM provider's validity period (Specified in mins)
$objSmsMessage.BodyFormat      = $objSmsConstants.BODYFORMAT_TEXT 

Step 5: Send SMS messages

The following code shows how to send an SMS message using the data that was stored in the 'objSmsMessage' and 'objSmsConstants' objects. After the message is sent the error result will be displayed. Error 0 means the message was sent succesfully.

# Send the Message
write-host "Sending the Message..."
$objGsm.SendSms($objSmsMessage)
$strResult = "SendSms, result: " + $objGsm.LastError + " (" + 
                                   $objGsm.GetErrorDescription($objGsm.LastError) + ")"
write-host $strResult

Appendix: Full source code

Following you can find the full source code which is also included in the ActiveXperts SMS Component package.

# ***************************************************************************
#
# ActiveXperts SMS Component
#
# Send a text SMS message through a connected GSM phone or modem.
# The GSM phone or modem must be connected to your computer.
#
# (c) Copyright ActiveXperts Software - www.activexperts.com
#
# ***************************************************************************

# ***************************************************************************
# Function AskDevice
# ***************************************************************************
Function AskDevice ( $objGsm )
{  
  $strMessage = "Connected devices:"
  
  $strDevice = $objGsm.FindFirstDevice()
  
  While ($objGsm.LastError -eq 0)
  {     
     $strMessage += "`r`n" + $strDevice
     $strDevice = $objGsm.FindNextDevice()
  }
  
  # Add COM Ports.
  # Get first COM Port.
  $strPort = $objGsm.FindFirstPort()
  
  While ($objGsm.LastError -eq 0)
  {
    $strMessage += "`r`n" + $strPort
    $strPort = $objGsm.FindNextPort()
  }

  $strDefaultDevice = $objGsm.FindFirstDevice()
  
  If ($objGsm.LastError -ne 0)
  {
    $strDefaultDevice = $objGsm.FindFirstPort()
  } 
  
  # Display Devices
  if ($strDefaultDevice -ne $Empty)
  {
    write-host $strMessage
    write-host $Empty
    $strSelectedDevice = ReadInput "Select Device:" $strDefaultDevice $False
  }  

  return $strSelectedDevice 
}

# ***************************************************************************
# Function ReadInput
# ***************************************************************************
Function ReadInput($strPrompt, $strDefaultValue, $bAllowEmpty)
{ 
  $strReturn = ""  
  If ($strDefaultValue -ne "")
  {
     $strPrompt += " leave empty for " + $strDefaultValue
  }
  
  Do 
  {       
    write-host $strPrompt
    $strReturn = read-host
    
    If ($strReturn -eq "" -and $strDefaultValue -ne "")
    {
      $strReturn = $strDefaultValue
      write-host $strReturn
    }
    elseif ($strReturn -eq "" -and $bAllowEmpty -eq $True)
    {
      break
    }   
  } While ($strReturn -eq "") 
  
  write-host ""
  return $strReturn
}

# ***************************************************************************
# Function AskStatusReport
# ***************************************************************************
Function AskStatusReport()
{
  $title = "Status Report"
  # Please note: it can take minutes or even hours before a status report is received
  $message = "Do you want to wait for a status report after submitting the message?"

  $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
      "The script will request and wait for a status report."

  $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
      "The script will not request and wait for a status report."

  $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

  $result = $host.ui.PromptForChoice($title, $message, $options, 1) 
  
  switch ($result)
      {
          0 { return $True}
          1 { return $False}
      }
}

# ***************************************************************************
# The script itself
# ***************************************************************************
cls

$objGsm          = new-object -comobject AxSms.Gsm
$objSmsMessage   = new-object -comobject AxSms.Message
$objSmsConstants = new-object -comobject AxSms.Constants

# Display SMS Component Version
write-host "SMS Component Version" $objGsm.Version "; "
                                      "Build" $objGsm.Build "; "
                                     "Module" $objGsm.Module

# Display if the ActiveXperts SMS Component is registered.
write-host "License Status:" $objGsm.LicenseStatus
write-host ""

# Set Logfile
$objGsm.Logfile = $env:temp + "\GSM_sendsms_text.txt"
write-host "Log file can be found here:"
write-host $objGsm.Logfile
write-host ""

# Select Device
$strDevice = AskDevice( $objGsm )

If ($strDevice -eq $Empty)
{
  write-host "No devices or COM ports where found."
  write-host "Ready."
  exit  
}

# Open connection to the device
$objGsm.Open($strDevice)

$strResult = "Open, result: " + $objGsm.LastError + " (" + 
                                $objGsm.GetErrorDescription($objGsm.LastError) + ")"
write-host $strResult
#36101 means: modem requires pin code, see also: www.activexperts.com/support/errorcodes
If ($objGsm.LastError -eq 36101) 
{
  # Enter PinCode 
  $strPin = ReadInput "Enter PIN code" "" $False
  $objGsm.Open($strDevice, $strPin)
  $strResult = "Open, result: " + $objGsm.LastError + " (" + 
                                  $objGsm.GetErrorDescription($objGsm.LastError) + ")"
  write-host $strResult
}

If ($objGsm.LastError -ne 0)
{ 
  $objGsm.Close()
  write-host "Ready."  
  exit
}

# Message: set all properties
$objSmsMessage.Clear()
$objSmsMessage.ToAddress       = ReadInput "Enter Recipient" "" $False
$objSmsMessage.Body            = ReadInput "Enter the message text:" "Hello, world!" $False
$objSmsMessage.ValidityPeriod  = 0 # Use GSM provider's validity period (To be specified in mins)
$objSmsMessage.BodyFormat      = $objSmsConstants.BODYFORMAT_TEXT 

$objSmsMessage.RequestDeliveryReport = AskStatusReport
$bSearchStatusReport = $objSmsMessage.RequestDeliveryReport

# Send the Message
write-host "Sending the Message..."
$objGsm.SendSms($objSmsMessage)
$strResult = "SendSms, result: " + $objGsm.LastError + " (" + 
                                   $objGsm.GetErrorDescription($objGsm.LastError) + ")"
write-host $strResult
$strReference = $objSmsMessage.Reference
If ($objGsm.LastError -ne 0)
{
  $objGsm.Close()
  exit
}

# Show the message Reference
write-host "Message Reference (can be used with status reports):"$strReference

# Wait for status report
While ($bSearchStatusReport)
{
  write-host "Receiving status report..."
  $objGsm.Receive($objSmsConstants.GSM_MESSAGESTATE_ALL, $False, 
                  $objSmsConstants.GSM_STORAGETYPE_ALL, 25000)
  $strResult = "Receive, result: " + $objGsm.LastError + " (" + 
                                     $objGsm.GetErrorDescription($objGsm.LastError) + ")"
  write-host $strResult
  
  If ($objGsm.LastError -ne 0)
  {
    break;
  }
  
  $objStatusReport = $objGsm.GetFirstReport()
  
  While ($objGsm.LastError -eq 0)
  {
    If ($objStatusReport.Reference -eq $strReference)
    {
      $bSearchStatusReport = $False
      write-host "Status Report"$objStatusReport.Referece"received!"
      $objGsm.DeleteReport($objStatusReport)
      $strResult = "DeleteReport, result: " + $objGsm.LastError + " (" + 
                                              $objGsm.GetErrorDescription($objGsm.LastError) + ")"
      write-host $strResult
      break;
    }
    else
    {
      $objStatusReport = $objGsm.GetNextReport()
    }
  }
  
  If($bSearchStatusReport)
  {
    write-host "Status report not found yet."
    start-sleep 3
  }
}

$objGsm.Close()
write-host "Ready."
start-sleep 3
exit

You can download the full source code of this project from the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/sms-component. There are many other working samples included with the product or on the FTP site.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The ActiveXperts SMS Component project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.