【问题标题】:C# Powershell Pipeline Import Module not workingC# Powershell 管道导入模块不工作
【发布时间】:2021-02-11 01:29:07
【问题描述】:

我正在尝试导入 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 Runspace 时一定遗漏了一些东西。有人有想法么?提前致谢!

【问题讨论】:

    标签: c# powershell runspace


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 2016-11-12
      • 2011-08-03
      • 1970-01-01
      相关资源
      最近更新 更多