Add User to Lync using C# (Enable-CSUser Powershell)

The current project I’m working on has a web-based application that manages user provisioning – add/edit/delete – to Active Directory, Microsoft Exchange and Lync (previously OCS).

The way to provision a user within Lync is via Powershell – lots of bingoogle’ing tells that you can use the Enable-CsUser command – this is what I need to do via Powershell :

Enable-csUser -identity AccountName -RegistrarPool lyncPoola.FQDN -SipAddressType EmailAddress

So – how do you do it via C# ??   

There doesn’t seem to be many people who have been able to do this – judging by the Q&A via StackOverflow.

My choice of solution was to define a “web service” project – and can thus have this running *ON* the same server as Lync – thereby running ‘locally’ if needed.

(1) Include the DLL for Powershell

  • Install the Powershell SDK – and then grab the DLL and include in your project  
  • System.Management.Automation.dll

(2) Add the using statements :

  • using System.Management.Automation;
  • using System.Management.Automation.Runspaces;
  • using Microsoft.PowerShell;
  • IMPORTANT – for some reason, this doesn’t work in Visual Studio 2008 – so use Visual Studio 2010 instead.

(3) Open A PowerShell RunSpace

Need to import the “module” for PowerShell Lync commands – just like an “Import-Module” command.

  • InitialSessionState iss = InitialSessionState.CreateDefault();
  • iss.AuthorizationManager = new AuthorizationManager("MyShellId");  
  • iss.ImportPSModule(new string[] { "Lync" });
  • Runspace powerShellRunspace = RunspaceFactory.CreateRunspace(iss);
  • powerShellRunspace.Open();

*** You might get an error like this :

"File C:Program FilesCommon FilesMicrosoft Lync Server 2010ModulesLyncstartup.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details

The answer I found was to include the line above with the “AuthorizationManager” – this one line of “awesome (!)” was thanks to Nivot Ink.

This essentially ‘bypasses’ the ExecutionPolicy checks – proceed with caution !

(4) Do the PowerShell Command

This uses the PowerShell RunSpace which has been initiated with the “Lync” module.

The code then defines the “Command” – ie. Enable-CsUser.

The AddParameter is then like the Params for any method – like a Stored Procedure (SQL) – or something like that.   

Prepare your key/value parameters – and then ‘fire’ using the “Invoke” command.

The results are returned as a “PSObject” collection – so just save these to a StringBuilder to return.

  • StringBuilder stringBuilder = new StringBuilder();
  • using (PowerShell powershell = PowerShell.Create())
  • {
  • powershell.Runspace = powerShellRunspace;
  • powershell.AddCommand("Enable-CsUser");
  • powershell.AddParameter("Identity", samName);
  • powershell.AddParameter("RegistrarPool", “lyncPoola.FQDN”);
  • powershell.AddParameter("SipAddressType", "EmailAddress");
  • Collection<PSObject> results = powershell.Invoke();
  • foreach (PSObject obj in results)
  • {
  • stringBuilder.AppendLine(obj.ToString());
  • }
  • powerShellRunspace.Close();
  • }
  • return stringBuilder.ToString()

(5) Do a Happy Dance !!

This was fairly tricky to get working – took me a day or so to get the bits aligned.

Other considerations :

  • Need to run the code as a user with Lync admin permission
  • Need to run the code from a server/machine that is “on the domain”

Here’s a full set of the source code that I used :

image

Let know if this helped you – as it was a good challenge to crack !

Smile

Advertisements

3 thoughts on “Add User to Lync using C# (Enable-CSUser Powershell)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s