A while ago I did a blog post and it leverage the Lync Client SDK  to do call testing but it was dependant on the users being in your contact list. This is ok to a degree but what if you needed to just provide an input list of sip uri’s that you needed to query.

So I updated it :)

The main 2 changes are I put it into a function and I used a sub-method that is under the $client.contactmanager called GetContactByURI. this method requires a single line of text to search for and it can return the contact object that we need to add to an IM or a call etc.. the $client btw is the instance of the lync client that is running.

refer to the previous post for most of the explanation but I have tried to comment the code out pretty well.

<#
.SYNOPSIS
Runs an automated script to send a user an IM message, followed by a phone call. If the user picks up it puts the call on hold, plays a recorded file and hangs up
.DESCRIPTION

  Create a file called inputfile.txt and enter each sipuri line by line, this script will test each user and generate a report at the end of it
.PARAMETER Inputfile

.EXAMPLE
.\LyncAutomatedTest.ps1 -inputfile inputfile.txt
#>

[CmdletBinding()]
param([Parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string]$inputfile)

function TestSIPURI($sipuri,$client)
{

    write-verbose "Testing For $sipuri has begun"
    $conversation = $client.ConversationManager.AddConversation()
    $contact = $client.ContactManager.GetContactByUri($sipuri)
    $null= $conversation.AddParticipant($contact)

    #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... The Lync System Has Been Patched. Your desktop phone will be rung shortly to check its operation, The call will connect be put on hold where you will here some music and then end please wait until it completes")
    #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
    sleep 20

    #for audio call changing the modality tpype
    $ModalityType = 2
    $message = $conversation.Modalities[$ModalityType]
    write-host "Making Phone Call"
    $null = $message.BeginConnect($null,$null)
    sleep 20
    if ($message.state -eq "Connected")
    {
        $usercontact = $contact.uri.split(":")[1]
        write-host "Successfully Connected Voice Call to:`t $usercontact"
        #Idea to playback a WMA file which says the system is testing itself
        sleep 10
        write-host "Switching Call to OnHold For Playback Music"
        $message.BeginHold($null,$null)
    }
    else
    {
        $usercontact = $contact.uri.split(":")[1]
        write-host "Could Not Connect a Voice Call to:`t $usercontact"
    }
    sleep 10
    write-host "Ending Call"
    #end the call
    $message.Conversation.End()
}

#########################################Main Script Code#########################################

cls
write-host "##################################################################"
write-host "#             Welcome to the Automated Test Call Script          #"
write-host "#                  We will begin the sequence now                #"
write-host "##################################################################"

write-verbose "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)
{
    Write-Verbose "checking is assemblies are installed and at the correct path"
    $assemblyPath = "C:\Program Files (x86)\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.dll"
    $assemblyexist = test-path $assemblyPath
    if ($assemblyPath -eq $false)
    {
        cls
        Write-Error -Message "Lync SDK Not Detected Please Download it From http://www.microsoft.com/en-IE/download/details.aspx?id=36824"
        exit 0
    }
        Write-Verbose "importing assembly"
        Import-Module $assemblyPath
}

#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

Write-Verbose "getting users from $inputfile"
$users = get-content $inputfile
write-host "Processing Users please be patient..."
foreach ($User in $users)
{
    Write-host "Processing $User"
    $result = TestSipURI $user $client
}

write-host "Finished"
write-host "Please check $reportfile for all results"