Created
December 12, 2013 15:50
-
-
Save lidopaglia/7930170 to your computer and use it in GitHub Desktop.
Adds a user's department colleagues to Lync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -version 3 | |
<# | |
.SYNOPSIS | |
Adds a user's department colleagues to Lync | |
.DESCRIPTION | |
This script is designed to execute under the context of the currently logged on user. The script will make a call to Active Directory | |
to find the department of the currently logged on user and will look for all users with the same department. If there are users with | |
the same department as the currently logged on user we get only the users that are Lync enabled. We then check if the currently logged | |
on user’s contact list already has a group with the same name as the currently logged on user’s department. If such a group exists we | |
add the user’s department colleagues to their Lync contacts only if they’re missing from the contact group. If the group doesn’t exist | |
at all we create it and populate it. | |
.NOTES | |
Author: Lido Paglia <[email protected]> | |
Date: 12/11/2013 18:07:24 | |
Comments: Requires the Lync 2013 SDK DLLs to be available. Modify the $AssemblyPath variable to point to the correct location of the | |
DLLs on the client workstation. | |
#> | |
[cmdletbinding()] | |
Param() | |
$AssemblyPath = "${env:ProgramFiles(x86)}\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.dll" | |
Import-Module $AssemblyPath | |
function Get-LyncGroup | |
{ | |
[cmdletbinding()] | |
Param( | |
[Parameter(Position=0)] | |
[string]$Name | |
) | |
begin | |
{ | |
if(-not(Get-Module -Name Microsoft.Lync.Model)) | |
{ | |
#Lync model dll not loaded! | |
$AssemblyPath = "${env:ProgramFiles(x86)}\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.dll" | |
try | |
{ | |
Import-Module $AssemblyPath -Force | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
} | |
try | |
{ | |
# Obtain the entry point to the Lync.Model API | |
$client = [Microsoft.Lync.Model.LyncClient]::GetClient() | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
$groups = $client.ContactManager.Groups | |
} | |
process | |
{ | |
if($name) | |
{ | |
$customGroup = $groups.GetEnumerator() | Where {$_.Type -eq 'CustomGroup' -and $_.Name -eq $Name} | |
if($customGroup) | |
{ | |
[pscustomobject]@{ | |
Name = $customGroup.Name | |
Type = $customGroup.Type | |
Id = $customGroup.Id | |
Members = @($customGroup.Uri) | |
} | |
} | |
} | |
else | |
{ | |
$groups.GetEnumerator() | % { | |
[pscustomobject]@{ | |
Name = $_.Name | |
Type = $_.Type | |
Id = $_.Id | |
Members = @($_.Uri) | |
} | |
} | |
} | |
} | |
} | |
function New-LyncCustomGroup | |
{ | |
[cmdletbinding()] | |
Param( | |
[Parameter(Mandatory=$true,Position=0)] | |
[string]$Name | |
) | |
begin | |
{ | |
if(-not(Get-Module -Name Microsoft.Lync.Model)) | |
{ | |
#Lync model dll not loaded! | |
$AssemblyPath = "${env:ProgramFiles(x86)}\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.dll" | |
try | |
{ | |
Import-Module $AssemblyPath -Force | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
} | |
else | |
{ | |
try | |
{ | |
# Obtain the entry point to the Lync.Model API | |
$client = [Microsoft.Lync.Model.LyncClient]::GetClient() | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
} | |
$state=New-Object -TypeName System.Object | |
$callback = [AsyncCallback]{ | |
#http://stackoverflow.com/questions/16281955/using-asynccallback-in-powershell | |
param($asyncResult) | |
# callback code | |
if ($asyncResult.isCompleted) | |
{ | |
$client.ContactManager.EndAddGroup($asyncResult) | |
} | |
} | |
} | |
process | |
{ | |
try | |
{ | |
$client.ContactManager.BeginAddGroup($Name,$callback,$state) | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
} | |
} | |
function Add-LyncContact | |
{ | |
[cmdletbinding()] | |
Param( | |
[Parameter(Mandatory=$true,Position=0)] | |
[string]$ContactUri, | |
[Parameter(Mandatory=$true,Position=1)] | |
[string]$GroupName | |
) | |
begin | |
{ | |
if(-not(Get-Module -Name Microsoft.Lync.Model)) | |
{ | |
#Lync model dll not loaded! | |
$AssemblyPath = "${env:ProgramFiles(x86)}\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.dll" | |
try | |
{ | |
Import-Module $AssemblyPath -Force | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
} | |
try | |
{ | |
# Obtain the entry point to the Lync.Model API | |
$client = [Microsoft.Lync.Model.LyncClient]::GetClient() | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
$myGroup=$client.ContactManager.Groups | Where-Object {$_.Name -eq $GroupName} | |
$state=New-Object -TypeName System.Object | |
$callback = [AsyncCallback]{ | |
#http://stackoverflow.com/questions/16281955/using-asynccallback-in-powershell | |
param($asyncResult) | |
# callback code | |
if ($asyncResult.isCompleted) | |
{ | |
$mygroup.EndAddContact($asyncResult) | |
} | |
} | |
} | |
process | |
{ | |
#lookup the contact in lync | |
$contact = $client.ContactManager.GetContactByUri($ContactUri) | |
if($contact) | |
{ | |
try | |
{ | |
#$client.ContactManager.BeginAddGroup($Name,$callback,$state) | |
$myGroup.BeginAddContact($contact,$callback,$state) | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
} | |
} | |
} | |
function Get-ADUser | |
{ | |
Param( | |
[string]$UserName=$env:USERNAME | |
) | |
$filter = "(&(objectCategory=User)(SamAccountname=$UserName))" | |
$Domain = New-Object System.DirectoryServices.DirectoryEntry | |
$Searcher = New-Object System.DirectoryServices.DirectorySearcher | |
$Searcher.SearchRoot = $Domain | |
$Searcher.PageSize = 1000 | |
$Searcher.Filter = $filter | |
$Searcher.SearchScope = "Subtree" | |
$Searcher.FindOne() | |
} | |
function Get-ADDepartment | |
{ | |
Param( | |
[string]$Department | |
) | |
$filter = "(&(objectCategory=User)(Department=$Department))" | |
$Domain = New-Object System.DirectoryServices.DirectoryEntry | |
$Searcher = New-Object System.DirectoryServices.DirectorySearcher | |
$Searcher.SearchRoot = $Domain | |
$Searcher.PageSize = 1000 | |
$Searcher.Filter = $filter | |
$Searcher.SearchScope = "Subtree" | |
$Searcher.FindAll() | |
} | |
$myADUser = [ADSI](Get-ADUser).Path | |
$myDepartment = $myADUser.department | |
$colDeptMembers = Get-ADDepartment -Department $myDepartment | |
if($colDeptMembers) | |
{ | |
$CoWorkers = $colDeptMembers.getDirectoryEntry() | |
#get the user's department contacts | |
$Contacts = $CoWorkers | Where-Object {$_.'msRTCSIP-PrimaryUserAddress' -and $_.samaccountname -ne $myADUser.samaccountname} | | |
Select-Object -ExpandProperty 'msRTCSIP-PrimaryUserAddress' | | |
Sort-Object | |
Write-Verbose "$($contacts.count) department Lync contacts found for `'$myDepartment`'" | |
if($GroupExists = Get-LyncGroup | Where {$_.Name -eq $myDepartment}) | |
{ | |
Write-Verbose "$myDepartment Lync Group exists" | |
#if contact is missing add it. Never remove a contact. | |
$NewContactResult = $Contacts | foreach { | |
if($_ -notin $GroupExists.Members) | |
{ | |
Add-LyncContact -GroupName $myDepartment -ContactUri $_ | |
} | |
} | |
} | |
else | |
{ | |
Write-Verbose "$myDepartment Lync Group does not already exist" | |
#create the group | |
$NewGroupResult = New-LyncCustomGroup -Name $myDepartment | |
while(-Not($NewGroupResult.IsCompleted)) | |
{ | |
Start-Sleep -Milliseconds 30 | |
} | |
# Need to research more appropriate way to handle this. | |
# | |
# Not pausing here throws a runtime exception of attempting to call a method on a | |
# null-valued expression since we try adding contacts to a group that doesn't yet exist. | |
# | |
# In testing 2 seconds was long enough to wait. | |
Start-Sleep -Seconds 2 | |
# Add all found dept lync contacts | |
$NewContactResult = $Contacts | foreach { | |
Add-LyncContact -GroupName $myDepartment -ContactUri $_ | |
} | |
} | |
Write-Verbose "Completed adding contacts." | |
} | |
else | |
{ | |
Write-Verbose "Didn't find any contacts to add." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script... Possible to tweak it, to have the ability to add all contacts in all departments in company? Example.. Script to create the contact group based on department, then add all users from each?