REEDIT: Original Credit to http://blogs.technet.com/b/nexthop/archive/2011/05/05/sendim.aspx nexthop guys… I add the audio part which is not that hard 😉
Today I want to show you a script that will automate the testing of IM and Audio Calls. Think if you have critical systems that you need to know that the handsets re-registered properly after a system reboot or patch.
if they don't answer at least you will know you need to check up on some stuff.
First we start off with taking in a SIP URI and checking if the Lync SDK is installed (download it from http://www.microsoft.com/en-IE/download/details.aspx?id=36824) after which we will load the assembly we need to run it
Next we need specify the contact object we want to search on, changing the number has different effects, 50 standards for SIP URI and as this is the most useful in this case it is what we will use. Also we need to reference the Lync Instance which is running currently using the [Microsoft.Lync.Model.LyncClient]::Getclient()
Once we do this we then create a conversation, but it is in active so no window will pop up
On Line 30 we enumerate the groups and all objects contained within to search on, in the next section of code we cycle through the objects until we match our input SIP URI
This gets the complete contact object for the user and allows us to add it to the conversation.
Next we can actually send an IM to this user
$IMType references that this is an instance message, as you will shortly 2 means Audio and so on… this essentially selects the modality for the conversation.
On line 59 you can customized this text to whatever you want.
On Line 63 is the process of sending the IM, at this point you will see a windows pop up with you message
we put the script to sleep for 20 seconds because we want someone to have a change to see the message before proceeding to calling them!
Next we change the modalitytype to audio (i.e. 2) and then make a call. The sleep 20 again is to allow time for the call to connect. we can check the state of the call and determine if it is connected. In this example I simply write a message but I could as easily send a mail or generate a further alert or trigger a workflow
Finally we end the call with the last bit of code
here is the complete code in text
#check if the module is loaded
$module = get-module -Name "Microsoft.Lync.Model"
#if module is not loaded check it exists and then load else exit
if ($module -eq 0)
$assemblyPath = “C:\Program Files (x86)\Microsoft Lync\SDK\Assemblies\Desktop\Microsoft.Lync.Model.DLL”
$assemblyexist = test-path $assemblyPath
if ($assemblyPath -eq $false)
Write-Error -Message "Lync SDK Not Detected Please Download it From http://www.microsoft.com/en-IE/download/details.aspx?id=36824"
#use [Microsoft.Lync.Model.ContactInformationType]:: Value here to get the contacttype field you want to use
#50 stands for SIP URI
$contacttype = 50
#Get the client instance we are working with this is the current process lync is operating under
$client = [Microsoft.Lync.Model.LyncClient]::GetClient()
#we start to build a conversation here in an inactive state and we can add stuff to it
$conversation = $client.ConversationManager.AddConversation()
#capture our groups and the clients within
$groups = $client.ContactManager.Groups
#cycle the groups for the contact object we want
$i = 0
foreach ($group in $groups)
foreach ($contact in $group)
if ($contact.GetContactInformation($contacttype) -eq $sipuri)
if ($i -gt 0)
#First Send an Instant Message so we need the InstantMessage Modality
$IMType = 1
$PlainText = 0
$data = New-Object "System.Collections.Generic.Dictionary[Microsoft.Lync.Model.Conversation.InstantMessageContentType,String]"
$data.Add($PlainText, "This is an Automated Test… ")
#This sets the modality type
$message = $conversation.Modalities[$IMType]
write-host "Sending Status IM"
$null = $message.BeginSendMessage($data, $null, $data)
#Sleeping to allow the instant message to go through
#for audio call changing the modality tpype
$ModalityType = 2
$message = $conversation.Modalities[$ModalityType]
write-host "Making Phone Call"
$null = $message.BeginConnect($null,$null)
if ($message.state -eq "Connected")
write-host "Successfully Connected Voice Call to:`t" $contact.uri.split(":")
#Idea to playback a WMA file which says the system is testing itself
write-host "Switching Call to OnHold For Playback Music"
write-host "Could Not Connect a Voice Call to`t:" $contact.uri.split(":")
write-host "Ending Call"
#end the call