# encoding: ascii
# api: powershell
# title: Send-SMSMessage
# description: Function to send text messages (SMS) from Windows PowerShell using the Microsoft Outlook SMS Add-in.
# version: 0.1
# type: function
# author: Jan Egil Ring
# license: CC0
# function: Send-SMSMessage
# x-poshcode-id: 1999
# x-archived: 2016-10-18T10:42:50
# x-published: 2010-07-21T08:04:00
#
# For more information, see the following blog-post:
# http://blog.powershell.no/2010/07/21/send-text-messages-sms-using-windows-powershell
#
#requires -version 2

function Send-SMSMessage {
	<#
.SYNOPSIS
Send a Text Message (SMS) using Microsoft Outlook
.DESCRIPTION
Sends a Text Message (SMS) using the supplied parameters.
.PARAMETER To
Telephone number to send the text message to.
.PARAMETER Message
The message to send.
.EXAMPLE
Send-SMSMessage -To 555-12345 -Message "This is a test message"
.NOTES
Requires Windows PowerShell v2 and Microsoft Office Outlook 2010.
Not tested in Outlook 2003 or 2007, but it should work with the Microsoft Outlook SMS Add-in installed (download from Microsoft).
AUTHOR:    Jan Egil Ring
BLOG:      http://blog.powershell.no
LASTEDIT:  21.07.2010 
#>

	[CmdletBinding()]
	param (
	[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
	$To, 
	[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
	$Message 
	)

	#Check if Outlook are running
	$OutlookState = Get-Process | Where-Object {$_.Name -eq "outlook"}

	#Check whether Outlook is installed
	if (-not (Test-Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE")) {
		Write-Warning "Outlook are not installed. The message was not sent."
		break
	}

	#Check wheter an Text Messaging (SMS) account are set up in Outlook
	$outlook = New-Object -ComObject outlook.application
	if (-not (($outlook.Session.Accounts | Where-Object {$_.AccountType -eq "5"} | Measure-Object).Count -gt 0)) {
		Write-Warning "Outlook are installed, but no accounts are configured for Text Messaging (SMS). The message was not sent."
		break
	}


	#Send message
	$NewMessage = $outlook.CreateItem("olMobileItemSMS") 
	$NewMessage.To = $To
	$NewMessage.Body = $Message
	$NewMessage.Send($true)

	#Close outlook.exe if it was not running before executing this function   
	if (-not $OutlookState) {
		$outlook.Quit()
	}


}
