Lync Server and PowerShell – My favourite features, oneliners and scripts

I was preparing to speak at Microsoft Technology User Group (MTUG) here in Norway at the PowerShell Script Club. My session for the night was going to be Lync Server Management Shell. First I thought I was not going to prepare anything and wing the whole session. The more I thought about it, winging any session that I am going to give in front of an audience is seldom a good idea. So I thought, ok make a blog post instead. I will here add my favourite features, oneliners and scripts that I have found working with Lync Server. I will update this blogpost as I remember and find new features, oneliners and scripts. Please let me know of your best PowerShell tricks in Lync.

Last updated 17.01.2012

Connecting

In addition to log on the Lync Server and open Lync Server Management Shell you can also access and work with Lync through remote PowerShell which came available in V2. Here is what you need to connect. The url could be your external webservices url as well

$session = New-PSSession -ConnectionUri https://lync-admin.contoso.local/OcsPowershell -Credential (Get-Credential)
Import-PSSession -Session $session

Finding cmdlets

There are several ways to find cmdlets in Lync. If you like the graphical representation like a mind map there is some good ones created by MVP Tom Arbuthnot

In PowerShell there is some cmdlets available as well when finding what is available

#Get all cmdlets for Lync that contains -Cs
Get-Command *-Cs* -CommandType cmdlet -Module Lync
#Get all cmdlets that contains user
Get-Command *user* -CommandType cmdlet -Module Lync

#When you find the cmdlet you want to use find the syntax on how to use it
Get-Help Get-CsUser -Examples
#Use -Online to open the TechNet website for that cmdlet
Get-Help Get-CsUser -Online

#Looking for a specific setting to set and don't know which cmdlet that can modify it use the below code
$params = Get-Command -CommandType Cmdlet *-Cs* | % { $n = $_.Name ; $_.Parameters.Values | % { Add-Member -in $_ noteproperty CmdletName $n; $_ } }
$params | where { $_.Name -like "*meeting*" } | select Name,CmdletName
#This is cool, found the trick over at http://blogs.technet.com/b/csps/archive/2010/06/14/howtofindsetting.aspx

Cmdlets

There are some cmdlets I use more than others and find useful. Let me know what is your most used cmdlets.

#List a lot of the topology information, here you can sort to find URLs, ports and servernames configured
Get-CsService

#List all pools in your topology, useful for finding Front End pool name, when you forget it
Get-CsPool

#The only policy that is not available in Lync Control Panel, lot of nice features can be configured here
Get-CsClientPolicy

#Update the AddressBook files
Update-CsAddressBook

#List the CMS replication status for each Lync Server
Get-CsManagementStoreReplicationStatus

Oneliners

The most used oneliners that I use

#Find what Lync Services are running
Get-CsWindowsService -ExcludeActivityLevel | ft Name, Status
#Start any stopped services
Get-CsWindowsService -ExcludeActivityLevel | where {$_.Status -like "Stopped"} | Start-CsWindowsService

#Find all users that are enabled for Lync and have a LineUri, sort them by LineUri, display displayname and LineUri
Get-CsUser -Filter {LineURI -ne $Null} | sort -Property LineURI | ft DisplayName,LineURI

#Quickly find and open the share folder for the Lync pool
Invoke-Item(Get-CsService -FileStore | Select-Object -ExpandProperty UncPath)

Scripts

There are a lot of scripts that is created and expand on Lync Server PowerShell functionality and and some that simplifies working with Lync as well as monitors the solution. Below are some of my favourite scripts. Please let me know of other epic scripts out there.

Set-Lync2010Features.ps1

A script to install prerequisites on Server 2008 R2 before you start install Lync Server on the OS.  You can download the resource kit tools, Silverlitght and other tools easily. You even have som post deployment options in the script as well. This script is highly recommended and I use it in every deployment.

List-UnusedNumbers.ps1

A scripts that read unassigned numbers and depends on you having put you entire number serie there. It will find all numbers assigned to users, devices and features in Lync and find what numbers are available in any given unassigned numbers series. This is a script that saves you the pain of managing available numbers in an excel sheet.

Set-GlobalVoiceRouting.ps1

This script is for demo or initial deployments only. It is created for norwegian rules and genereates an easy way to dial out through one gateway and has the usual normalization rules for Norway. To edit the script and find the nomralization rules for your country see the Dialing Rule Optimizer.

Reset-UserPolicies.ps1

This script is used in an OCS to Lync migration scenario. It will reset all policies to $Null so that they use Global or Pool level policies for External access, Voice Policy and so on. The reason for this is that all users should use what you define for Lync and not what you inherit from OCS.

Get-CsConnections.ps1

This script lists user connections, client versions and the distribution of users in a load balanced scenario

Monitoring OCS and Lync Peak Call Capacity

Script to monitor how many concurrent calls a particular OCS or Lync Mediation Server is handling. The script grabs the counters for inboud and outbound calls, parses their values, adds them together, and dumps the output into a CSV file. A good tool to find how many concurrent calls you have on your deployment

Update-AdPhoto.ps1

A script that enables you to import AD photos from file and store it in the thumbnailPhoto attribute. It is a central feature in Lync to show a photo of users

New-SipContact.ps1

A script to enable users to search for external contacts and Video Conferencing endpoints from Lync. It creates a contact in AD and adds the SIP address to the msRTCSIP-PrimaryUserAddress so it will be synced to the addressbook in Lync.

Scripting Tips

There is a lot of techniques used for scripting. The best script tips I know of I will list here.

Use Write-Debug

Lets you easilly define debug lines that can test variables and logic in you script that is good to use when developing the script but that not need to be there when it is used in production. When you want to debug the script you change the $DebugPreference from ‘SilentlyContinue’ to ‘Continue’. Then all Write-Debug lines will be displayed.

$DebugPreference = 'Continue'

$var = Read-Host "Enter a computer name"
Write-Debug “’$var’ contains $var“

$DebugPreference = 'SilentlyContinue'

Use Functions

Functions are scripts in scripts. If it is certain things you need to do more than once in you script, make it a function with an input and output. I have also seen functions being used to organize your script in a better way to make it easier and more ordered to view.

Function Do-Something ($computername,$domainname) {
	# function code goes here
}

Resources

The Official Lync PowerShell Blog: http://blogs.technet.com/b/csps/p/categories.aspx
The Official Lync PowerShell Blog Cmdlet Descriptions: http://blogs.technet.com/b/csps/archive/2010/07/16/refallcmdlets.aspx
MVP Pat Richard’s Blog: http://www.ehloworld.com/category/powershell

3 thoughts on “Lync Server and PowerShell – My favourite features, oneliners and scripts

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.