1

I'm trying to create a PowerShell script that will send a Lync message to all users in column "A" the text in columns "B" and "C".

I started out using this guide- http://blogs.technet.com/b/csps/archive/2011/05/05/sendim.aspx

I believe I have most of the framework down, but my main issue is figuring out how to actually IM the user. When I run my script I get-

Cannot find an overload for "AddParticipant" and the argument count: "1".

Here's the code I'm using-

Function MassIM
{

Try
{
    $AssemblyPath = “C:\Program Files (x86)\Microsoft Lync\SDK\Assemblies\Desktop\Microsoft.Lync.Model.DLL”
    Import-Module $AssemblyPath
}
Catch [System.Exception]
{
    $TS = TimeStamp
        "Could not load required DLL files" | OutputToError
        FatalError
}

Try
{
    $LyncClient = [Microsoft.Lync.Model.LyncClient]::GetClient()
}
Catch [System.Exception]
{
    $TS = TimeStamp
    "Could not connect to Lync client." | OutputToError
        FatalError
}

Try
{
    $Excel = New-Object -ComObject Excel.Application
    $Excel.Visible = $False
}
Catch [System.Exception]
{
    $TS = TimeStamp
    "Could not load Excel." | OutputToError
        FatalError
}

Try
{
    $IMBook = $Excel.Workbooks.Open("C:\Users\cbrady-sa\Desktop\MassIM\List\IMList.xls")
}
Catch [System.Exception]
{
    $TS = TimeStamp
    "Could not open required workbook." | OutputToError
        FatalError
}

Try
{
    $IMSheet = $IMBook.Worksheets.Item(1)
}
Catch [System.Exception]
{
    $TS = TimeStamp
    "Could not open required worksheet." | OutputToError
        FatalError
}


    $FoundEmptyCell = $False
    $Row = 1

    While (!$FoundEmptyCell)
    {

        If ($IMSheet.Cells.Item($Row, 1).Value() -EQ $null)
        {
            $FoundEmptyCell = $True
        }
        Else
        {
            Try
            {
                $User = $IMSheet.Cells.Item($Row, 1).Value()
                $UserDesk = $IMSheet.Cells.Item($Row, 2).Value()
                $UserExt = $IMSheet.Cells.Item($Row, 3).Value()
            }
            Catch [System.Exception]
            {
               $TS = TimeStamp
               "Could not load data from worksheet." | OutputToError
               FatalError
            }

            Try
            {
                $User = $User -split ','
                $UserName = $User[1] + "." + $User[0] + "@xxx.com"
                $UserName.Trim()

                $Convo = $LyncClient.ConversationManager.AddConversation()

                $MSG = New-Object "System.Collections.Generic.Dictionary[Microsoft.Lync.Model.Conversation.InstantMessageContentType,String]"

                $Mod = $Convo.Modalities[1]

                $Convo.AddParticipant($UserName)

                $MSG.Add(0, "Hello ${UserName}. Is your desk still ${UserDesk}? Is your extension still ${UserExt}?")

                $null = $Mod.BeginSendMessage($MSG, $null, $MSG)

            }
            Catch [System.Exception]
            {
               $TS = TimeStamp
               "Could not instant message${UserName}." | OutputToError
               $Error | OutputToError
            }

            $Row++
        }
    }

$Excel.Quit()
}

Not sure why this isn't working; every guide I look at online seems to match what I have for the most part. Can't for the life of me figure out why I'm getting that error. This question appears similar but it has no real solution: Exception calling "BeginSendMessage" with "3" argument(s): "Value does not fall within the expected range."

Any help is immensely appreciated.

4

1 回答 1

0

With help from someone on another forum I was able to come up with the solution to my problem-

My first issue was trying to pass a string to "AddParticipant". "AddParticipant" expects a URI, so the first thing I had to do was convert my string to a URI via

$Contact = $LyncClient.ContactManager.GetContactByUri($UserName)

and then passing that to "AddParticipant" like so-

$null = $Convo.AddParticipant($Contact)

However, after that I started to get another exception-

"GetContactByUri" with "1" argument(s): "Value does not fall within the expected range."

So I ended up stepping through and outputting $UserName each time the code touched it and I figured out that $UserName.Trim() only OUTPUTS the trimmed string and does not store it (of course!), so I changed that bit of code to to $UserName = $UserName.Trim() and everything is working beautifully!

What was happening was I was taking "lastname, firstname" from a cell in Excel and formatting it so that it would be "firstname.lastname@domain.com" but due to there being a space after the comma I had to use .trim() to get rid of it, but I never considered how .trim() worked and so the variable was retaining the space character.

于 2014-08-07T18:43:59.040 回答