Tuesday, 3 March 2015

A Guide to PowerShell for Lync and Exchange Online

Hello readers.  As always I hope this finds you well.  This odd little post is just a brain dump of information I found when researching how to get some information out of Lync Online and Exchange online for a customer I am working with.  This customer was specifically interested in finding out if users were using Lync Online.

Those of you that are familiar with Office 365 know that there are a bunch of reports built into the Office 365 Admin Center.  Tim Woo does a great job describing what you get Here.  And below is a quick screen grab from the post.

While the information you get from the Admin Center is very good at telling you how many of your users are using it, for what and how often, it is lacking one important piece of information.  And that is which of your users are using it.  This is particularly important when you produce a report in the Admin Center that states that 400 users are logging in and showing as active, but you have 1,200 users enabled.  You need to know which users are using it.  Then you can concentrate on the other 800.  After all, a project to implement a UC platform such as Lync is only deemed as a success if the users adopt and use it.  So you definitely need to know this.

If you have Lync installed on-premises the information is a little easier to get.  After all it is your Active Directory and if you have the Monitoring Role installed and working, your SQL server.  So running reports on it is pretty straightforward.

If, however, you are running in Office 365 you only get the information Microsoft serve up to you.  As I mentioned above, this is just numbers.  You might need to know who and when for that matter.

The information you can get from the command which is the ultimate focus of this post is a list of users and the last date they logged in.  This tells you that they logged in and used any application which is available in your tenancy.  If you are interested in Lync specifically and the users also use Exchange and SharePoint etc, then this isn't for you.  Conversely, if all you use is Exchange and all you are interested in is Exchange then this is still pertinent.

Luckily, however, the customer I am working with is ONLY interested in, and using Lync Online.  So the information they will get is exactly what they are after to show who is using it and when they did.

Before you get started there are some prerequisites to running the commands.

First, you need PowerShell 3.0 or better.  To determine which version you have run PowerShell as Administrator and enter get-host.  As you can see below I have version 4 which is the standard version on Windows 8.1.  If you are on a lower version then 3 you need to download and install it. - Windows PowerShell 3.0

Finally you need to download and run the Windows PowerShell Module for Lync Online.  This is a small executable that saves the Lync Online Connector module to your computer.  Run this EXE.
Then in an elevated PowerShell run the following:
import-module LyncOnlineConnector
Once you have imported the Module you need to establish some sessions.

To start with you need to set the variable for your Office 365 Administrator credentials.  Run the above in the Elevated PowerShell. 
$cred = Get-Credential <name@domain.com>

It should pop up with a box populated with the admin username you enter.  Enter the password and click OK.  This saves the credentials as the variable $cred which will be used in other commands as you will see below.

Next, start a Lync Online Session, run these two lines.
$LyncSession = New-CSOnlineSession -Credential $cred

Import-PSSession $LyncSession -AllowClobber
Now you need to start an Exchange Online Session to give you access to some of the other Exchange based commands.  Run these two. 
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $ExchangeSession -AllowClobber
To make sure you have both sessions established run Get-PSSession | FT –AutoSize.  You should see the following.

Once you have established the sessions to Lync Online and Exchange Online you can run commands against the environment.  I won't go into detail about all of the command here, but here is a list of Lync Commands and here is a list of Exchange Commands.

There are 5 Lync Reporting Commands available as part of the Exchange Online Commands.  The information you get is the same as the information you get in the Office 365 Admin Centre.

  1. Get-CsActiveUserReport : shows statistics about the number of active Lync Online users over time.
  2. Get-CsConferenceReport : shows statistics about the conferences held in Lync Online.
  3. Get-CsAVConferenceTimeReport : shows statistics about the duration (minutes) used by audio and video conferences.
  4. Get-CsP2PSessionReport : shows statistics about peer-to-peer sessions that took place in Lync Online.
  5. Get-CsP2PAVTimeReport : shows statistics about the duration of audio and video in peer-to-peer Lync Online sessions.
Now for the bit that tells you who is using Lync and when they did.  

Bear in mind that I only have one user in my tenancy and I have a mailbox and my account is enabled for Lync.  I log on to the portal, Outlook Web Access, Admin Centre, Lync, Outlook, and SharePoint.  Once again, if your users log in to all and sundry in Office 365 and you are only interested in Lync statistics, this isn't for you.

What I'm using is a combination of Get-User to get a list of all users in my tenancy and I’m them piping that to a Get-MailboxStatistics which might only be available as my user has a mailbox.

To produce a list run the following:
Get-User -resultsize unlimited | Get-MailboxStatistics | select DisplayName, LastLogonTime
If you have a lot of users you will want to increase the screen buffer.

You can also sort the output as follow:

Get-User -resultsize unlimited | Get-MailboxStatistics | Sort-Object LastLogonTime -Descending | select DisplayName, LastLogonTime 
Get-User -resultsize unlimited | Get-MailboxStatistics | Sort-Object DisplayName -Descending | select DisplayName, LastLogonTime 
If you then want to export the results to a Text file you can use something like the following:
Get-User -resultsize unlimited | Get-MailboxStatistics | Sort-Object LastLogonTime -Descending | select DisplayName, LastLogonTime >C:\Scripts\LastLogonTime.txt
Or to CSV
Get-User -resultsize unlimited | Get-MailboxStatistics | Sort-Object LastLogonTime -Descending | select DisplayName, LastLogonTime | Out-File c:\Scripts\LastLogonTime.csv
Once you get the information in a file you can start to filter it and play with it.  But what you should get is a list of all of the users in Office 365 and the last time the logged on with their credentials.  If all they are using is Lync online then it should be an accurate representation of how many users are logging into it and how recently.

Once you are done with these session you need to remove the link to them.  You can either exit them one at a time. 
Remove-PSSession $LyncSession
Remove-PSSession $ExchangeSession

Or both at once
Get-PSSession | Remove-PSSession

Then just a double check to make sure you have exited the sessions.  Run Get-PSSession | FT –AutoSize one more time.

Hopefully you will have found this information useful.  As I said it is just a brain dump of information I collected and used in order to get my customer the information they needed.

Call to action : If any of you know a better way to get the information, please feel free to share and I'll add a reference to it and your post.

Any comments are always welcome.  Thanks for reading.