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.'
我到处都是 google,但找不到解决方案,我尝试将其全部放入一个字符串中并将其添加到 pipeline.commands。我已经像上面那样逐行拆分了,我什至让它把命令复制到文本中,这样我就可以 copy/paste 它进入 powershell 并且它可以工作。我一定是在设置 Powershell Runspace 时遗漏了一些东西。谁有想法?提前致谢!
而且,没关系,我想通了。很多事情,主要是我编码的方式。
- 我需要使用 Pipeline.Commands.addScript() 而不仅仅是添加。 2. 我显然忘记在文件 (x86) 之间放置一个 space。 100% 是我的错误,享受阅读和欢笑吧! :)
我正在尝试导入 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.'
我到处都是 google,但找不到解决方案,我尝试将其全部放入一个字符串中并将其添加到 pipeline.commands。我已经像上面那样逐行拆分了,我什至让它把命令复制到文本中,这样我就可以 copy/paste 它进入 powershell 并且它可以工作。我一定是在设置 Powershell Runspace 时遗漏了一些东西。谁有想法?提前致谢!
而且,没关系,我想通了。很多事情,主要是我编码的方式。
- 我需要使用 Pipeline.Commands.addScript() 而不仅仅是添加。 2. 我显然忘记在文件 (x86) 之间放置一个 space。 100% 是我的错误,享受阅读和欢笑吧! :)