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.