Sunday, February 5, 2012

Calling Lync Server 2010 Powershell cmdlets from C# (.NET)

To run Lync Server Management Shell cmdlets from C#, we need to import Lync module into our Powershell runspace before calling. We do a similar thing to initialize Exchange Server runspace in C# 
RunspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out warning);
So same is the case with Lync Server 2010 but slightly different. Here we import Lync Server module rather than adding snap-in to our runspace.


There are 2 solutions.


1.  Run the following command 


    pipeline.Commands.AddScript(@"Import-Module 'C:\Program Files\Common Files\Microsoft Lync Server 2010\Modules\Lync\Lync.psd1'"); 
in your c# code before running the actual cmdlet.  (assuming the Lync server is installed at default location)


2.  Import Lync module using InitialSessionState


This is even better. You need to add the following code at the time of initializing your runspace.

 InitialSessionState iss = InitialSessionState.CreateDefault();
 iss.ImportPSModule(new string[] { "Lync" });
 myRunSpace = RunspaceFactory.CreateRunspace(iss);
 myRunSpace.Open();
Now run any Lync server cmdlet using this runspace (myrunspace).


You could also just create a runspace and run a PowerShell command:
Collection<PSObject> runPowerShellScript()
       {
           Runspace runSpace = RunspaceFactory.CreateRunspace();
           runSpace.Open();
           Pipeline pipeLine = runSpace.CreatePipeline();
           string myPowerShellScript = "Import-Module Lync";
           pipeLine.Commands.AddScript(myPowerShellScript);
           pipeLine.Commands.Add("Out-String");
           Collection<PSObject> resultObjects = pipeLine.Invoke();
           //... run Lync cmdlets using the current runspace
           runSpace.Close();
           return resultObjects;
       }

Enjoy administrating Lync :)

Sunday, January 22, 2012

.NET Framework Mixed mode assembly loading error "Cannot be loaded in x.x runtime without additional configuration"

Problem:


Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.


Reason:


You are using an assembly reference (or a DLL) which was build in old version of .NET framework than the one your application is built on.


Workaround:


There are 2 possible solutions:



  1. You can revert back you application's Target Framework by going to project properties. Choose the framework in which the reference assembly or DLL is built e.g. ( 2.0). 


Now you need to rebuild your project.

2. Second Solution, which is better than above that you need to modify your project's app.config file. (if it exists in you project). If app.config file does not exist in your project, add it manually to your project by Add --> New Item --> General --> Application Configuration File.
Now modify it by addting this xml node into <configuration> node:

 <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
 </startup>

So it would now look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
    </startup>
</configuration>






Now rebuild your project. Hopefully it would work great.

Introduction

Hi All!
This blog is going to be answer some programming and tech issues.
Posts would be related to software development issues and some other tech queries.