簡體   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