Lync Powershell and C#

RSS comment feed13. February 2012 21:08 by Greg Thomas in Lync, Office365  //  Tags:   //   Comments

I've been doing a lot of Powershell lately, beyond the typical - find a command, run a command, execute the command - business.  I started off the with the following FREE pdf book - Master Powershell - if you are looking for a primer, this is a great place to spot.  I learned a lot about the language and how it really differs from more strongly typed languages like C# - everything is so fluid and dynamic.  If you don't believe how good Powershell can be, check out this post on using the Lync Client SDK INSIDE of Powershell... awesome.

My main goal was to figure out a better way to migrate a few couple thousand users over to Lync and set them up with some custom Client and Archiving policies.  In the end I was able to do it merely through using some of the existing cmdlets available in Lync;

 

Get-CsAdUser -Filter (Enabled -ne $True) | Enable-CsUser -RegistrarPool "entpool.mydomain.com" -SipAddresstype EmailAddress -SipDomain mydomain.com

Get-CsAdUser -Filter {Enabled -eq $True} | ForEach-Object {Set-CsUser -Identity $_.SipAddress -AudioVideoDisabled $true}

Get-CsAdUser -Filter {Enabled -eq $True} | ForEach-Object {Grant-CSClientPolicy -Identity $_.SipAddress -PolicyName "IM Only Policy"}

Get-CsAdUser -Filter {Enabled -eq $True} | ForEach-Object {Grant-CsArchivingPolicy -Identity $_.SipAddress -PolicyName "Archive Policy"}

 

But even before starting this I had not used the ForEach-Objech command or even undersood the real value behind piping ("$_.SipAddress" is a property rendered from the initial query that only returns Enabled Lync users.

So then I was off to Phase 2 of my dastardly plans for World Domination which was to build an IM stress test tool for Lync that takes a couple of parameters, floods the system and doesn't require me to create a bunch of users in Active Directory and/or ask people for their passwords.  To do this I'm using UCMA application endpoints that I will wire up at real-time and have them talk to each other. 

So of course when I build anything I always want it to be self-contained.  If I'm downloading free code, I want to install it and run with it.  Not have to do 10 things somewhere else before I can use it - enter Powershell and C#.  Having done this before with Exchange I was pretty confident that this wouldn't be too bad.  If you are doing this straight from Powershell (not auto-provisioning your endpoints via UCMA) the execution order looks a little like this;

 

New-CsTrustedApplicationPool -identity entpool.mydomain.com -Registrar entpool.mydomain.com

Enable-CsTopology

New-CsTrustedApplication -ApplicationId "IMTester" -TrustedApplicationPoolFqdn entpool.mydomain.com -Port 5081

Enable-CsTopology

New-CsTrustedApplicationEndpoint -ApplicationId urn:application:imtester -TrustedApplicationPoolFqdn entpool.mydomain.com -SipAddress sip:IMUserA@mydomain.com -DisplayName IMTesterA

 

Not too bad - create a TrustedApplicationPool (if one does not already exist), reflect the change back to your topology, create the application, again enable topology and then start creating your endpoints.  All-in-all pretty easy, until I got into the C# code.  

I initially started with the following code to start Powershell and bring in the Lync module;

 

if (_runspace == null)

                {

                    InitialSessionState state = InitialSessionState.CreateDefault();

                    _runspace = RunspaceFactory.CreateRunspace(state);

                    _runspace.Open();

                }

                _shell = PowerShell.Create();

                _shell.AddScript(@"import-module Lync");

                _shell.Commands.AddCommand("Out-String");

 

However when I did this I then started getting errors about the Powershell Code Policy and the module not actually being loaded (no notice given UNTIL I started doing something).  I dug around and found an associated post where buddy basically boiled it down to two ways of accessing Lync Powershell from C#, the full article is here, but the one I went with is here;

 

if (_runspace == null)

{

                    InitialSessionState state = InitialSessionState.CreateDefault();

                    state.ImportPSModule(new[] { "Lync" });

                    _runspace = RunspaceFactory.CreateRunspace(state);

                    _runspace.Open();

}

_shell = PowerShell.Create();

 

I'm still trying to decipher the two methods, I know I have used the first snipped successfully when working with Exchange 2007 and never encountered an error at all.  In addition to running the above "special" code snipped I had to modify the execution policy for Powershell on my server to "Unrestricted".

I'm not completely sold on these changes (one of those, keep making small changes until the experiment is so dirty you don't know if your problem mutated along the way) and right now I'm working on my dev server.  Later this week I plan on bringing the code back into the office to give'r a whirl to see if I encounter the same issues.

Office365: First Impressions

RSS comment feed24. April 2011 22:43 by Greg Thomas in Lync, Office365, Exchange  //  Tags:   //   Comments

After months of trying to get into playing with Office365, I finally got access to the BETA this week to start playing around with it.  (Really my access game through because it went Public Beta, but that's ok).  My first impression is that Office365 is very slick, very well put together and real simple to use.  When I received my email I immediately dove in and setup some users for email, created some documents and most importantly started playing around with Lync.  I didn't really focus much on the Office part as I have already played around with this with SkyDrive and it seemed pretty similar.

By far - this has been the simplest Lync setup I have ever done in my life.  On the first day I had some issues, which I don't know if they were a result of me missing a step, but this weekend I went in and tried again.  After configuring the user for Lync, downloading the client install, I was pretty much done, I logged into my client and was ready to take IM/phone calls.  In some cases I had to download the Microsoft Online Assistant to enable logins to Lync to work (which, on the main Lync Client SDK install, when it can't log you in the first time it prompts you for the install and then you're good to go).  I have easily setup 4 users in a matter of minutes with little to no battles with certificates and DNS.  The only thing I am trying to figure out now is if I can federate with other users and include them in my profile list and if that even can be done.

From a developer's standpoint, there is a ton of information out there on the topic of Sharepoint, Exchange and Lync.  In Exchange, you get the Exchange Managed Service API (which wraps Exchange Web Services) and with Lync you get access to the Lync Client SDK (no UCMA or server-side code available yet), but I'm sure if you wrote a little UCMA app to sit on your server you might be able to have it talk to those SIP URIs.  That's all conjecture at this point as really all I have been doing is play around with the interface to see what I can and can't do.

So for the above reasons, I did not get the blog updated this weekend.  Lesson for me - don't announce a public change to your site when you get access to a brand-new Beta!

 

Blog Grade for race.openjive.com