繁体   English   中英

C# Powershell 管道导入模块不工作

[英]C# Powershell Pipeline Import Module not working

我正在尝试导入 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 = “USER@DOMAIN.com”

$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 = \"USER@DOMAIN.com"\" ");
        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, $null, $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.'

我一直在谷歌周围,我找不到解决方案,我尝试将它全部放入一个字符串并将其添加到 pipeline.commands。 我已经像上面那样逐行拆分它,我什至将它复制到文本中,这样我就可以将它复制/粘贴到 powershell 中并且它可以工作。 在设置 Powershell 运行空间时,我一定遗漏了一些东西。 有人有想法么? 提前致谢!

而且,没关系,我想通了。 很多东西,主要是我编码的方式。

  1. 我需要使用 Pipeline.Commands.addScript() 而不仅仅是添加。 2. 我显然忘记在文件 (x86) 之间放置一个空格。 100% 我的错误,享受阅读和笑声::)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM