简体   繁体   中英

C# Powershell Pipeline Import Module not working

I am trying to import a Lync module to automatically send a message to a user. my Powershell script is pretty straight forward.

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) 

And it works flawlessly in Powershell. However when i throw it into C# its failing saying it cannot find the Module.

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();

It throws an error

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.'

I have been all around google and i cannot find a solution, I have tried throwing it all into one string and adding it to the pipeline.commands. I have split it up line by line like above, I even Made it copy the command out to text so i can copy/paste it into powershell and it works. I must be missing something in setting up Powershell Runspace. Anyone have any ideas? Thanks in advance!

And, Neverminded I figured it out. Multiple things, mainly the way I coded it.

  1. I needed to use Pipeline.Commands.addScript() not just Add. 2. I apparently forgot to put a space in between Files (x86). 100% My error, Enjoy the read and the laugh: :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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