我正在尝试创建一个PowerShell脚本,它将向“A”列中的所有用户发送Lync消息“B”和“C”列中的文本。
我开始使用本指南 - http://blogs.technet.com/b/csps/archive/2011/05/05/sendim.aspx
我相信我已经完成了大部分框架,但我的主要问题是弄清楚如何实际IM用户。当我运行我的脚本时,我得到了 -
Cannot find an overload for "AddParticipant" and the argument count: "1".
这是我正在使用的代码 -
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()
}
不确定为什么这不起作用;我在网上看到的每一个指南似乎都与我的大部分内容相符。不能为我的生活弄清楚为什么我会得到那个错误。这个问题看似相似,但没有真正的解决方案:Exception calling "BeginSendMessage" with "3" argument(s): "Value does not fall within the expected range."
非常感谢任何帮助。
答案 0 :(得分:0)
在另一个论坛上的某人的帮助下,我能够找到解决问题的方法 -
我的第一个问题是尝试将字符串传递给" AddParticipant"。 " AddParticipant"需要一个URI,所以我要做的第一件事就是通过
将我的字符串转换为URI$Contact = $LyncClient.ContactManager.GetContactByUri($UserName)
然后将其传递给" AddParticipant"像这样 -
$null = $Convo.AddParticipant($Contact)
然而,在那之后我开始得到另一个例外 -
"GetContactByUri" with "1" argument(s): "Value does not fall within the expected range."
所以每次代码触及它时我都会逐步完成并输出$ UserName,我发现$ UserName.Trim()只输出修剪过的字符串并且不存储它(当然!),所以我改变了$ UserName = $ UserName.Trim()的一些代码,一切都运行得很好!
发生了什么事我正在服用"姓氏,名字"从Excel中的单元格进行格式化,使其成为" firstname.lastname@domain.com"但由于逗号后面有空格我不得不使用.trim()去除它,但我从未考虑过.trim()如何工作,所以变量保留了空格字符。