问题描述
我正在尝试导入 Lync 模块以自动向用户发送消息。我的 Powershell 脚本非常简单。
Powershell
$assemblyPath = “C:\Program Files (x86)\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.DLL”
Import-Module $assemblyPath
$IMType = 1
$PlainText = 0
$cl = [Microsoft.Lync.Model.Lyncclient]::GetClient()
$conv = $cl.ConversationManager.AddConversation()
$username = “[email protected]”
$getuser = $cl.ContactManager.GetContactByUri($username)
$null = $conv.AddParticipant($getuser)
$msg = New-Object “System.Collections.Generic.Dictionary[Microsoft.Lync.Model.Conversation.InstantMessageContentType,String]”
$msg.Add($PlainText,“Assistance needed”)
$m = $conv.Modalities[$IMType]
$null = $m.BeginSendMessage($msg,$null,$msg)
它在 Powershell 中完美运行。然而,当我把它扔进 C# 时,它失败说它找不到模块。
C#
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add("Import-Module \"C:\\Program Files(x86)\\Microsoft Office 2013\\LyncSDK\\Assemblies\\Desktop\\Microsoft.Lync.Model.dll\"");
pipeline.Commands.Add("$IMType = 1 ");
pipeline.Commands.Add("$PlainText = 0 ");
pipeline.Commands.Add("$cl = [Microsoft.Lync.Model.Lyncclient]::GetClient() ");
pipeline.Commands.Add("$conv = $cl.ConversationManager.AddConversation() ");
pipeline.Commands.Add("$username = \"[email protected]"\" ");
pipeline.Commands.Add("$getuser = $cl.ContactManager.GetContactByUri($username) ");
pipeline.Commands.Add("$null = $conv.AddParticipant($getuser) ");
pipeline.Commands.Add("$msg = New-Object \"System.Collections.Generic.Dictionary[Microsoft.Lync.Model.Conversation.InstantMessageContentType,String]\" ");
pipeline.Commands.Add("$msg.Add($PlainText,\"Assistance needed with the Virtual Fitting Kiosk(GREEN)\") ");
pipeline.Commands.Add("$m = $conv.Modalities[$IMType] ");
pipeline.Commands.Add("$null = $m.BeginSendMessage($msg,$msg) ");
pipeline.Invoke();
System.Management.Automation.CommandNotFoundException: 'The term 'Import-Module "C:\Program Files(x86)\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.dll"' is not recognized as the name of a cmdlet,function,script file,or operable program. Check the spelling of the name,or if a path was included,verify that the path is correct and try again.'
我一直在 google 周围找不到解决方案,我尝试将其全部放入一个字符串中并将其添加到 pipeline.commands。我已经像上面那样逐行拆分了它,我什至让它将命令复制到文本中,这样我就可以将它复制/粘贴到 powershell 中并且它可以工作。我一定在设置 Powershell Runspace 时遗漏了一些东西。谁有想法?提前致谢!
解决方法
而且,没想到我想通了。很多东西,主要是我编码的方式。
- 我需要使用 Pipeline.Commands.addScript() 而不仅仅是 Add。 2. 我显然忘记在 Files (x86) 之间放置一个空格。 100%我的错误,享受阅读和笑声! :)