Created
May 9, 2017 18:05
-
-
Save Gunslap/0b33f893a1722450c5edcbf8a13b21fa to your computer and use it in GitHub Desktop.
This script will allow you to automate Lync (Skype for Business) number assignment. It will update the user's number in Lync, as well as AD.
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
############################# | |
# Modify User Lync Number # | |
############################# | |
<# | |
Description: | |
This script will allow you to automate Lync (Skype for Business) number assignment. | |
It will update the user's number in Lync, as well as AD. | |
If you use a spreadsheet to track number assignment you'll want to add sections to update that as well. | |
Prereqs: | |
Make sure the server running the script has the ActiveDirectory Module installed | |
The running account will also need AD edit permissions | |
The account will also need Lync edit permissions | |
Future Consideration: | |
- Include a switch and then if true, add voicemail as well (need Exchange permissions in that case) | |
- Consider checking to see if the user is Lync enabled or not? - "Enable-CsUser" | |
#> | |
Function ModifyUserLyncNumber { | |
param ( | |
[string]$userName = "TestUser", | |
[string]$action = "Add", | |
[string]$newNumber = "123-123-1234" | |
) | |
#Grab an AD reference to the user to modify | |
$Employee = '' | |
$ADObject = Get-ADUser $userName -Properties * | |
if($ADObject) | |
{ | |
$Employee = $ADObject | |
} | |
else | |
{ | |
Write-Output "No user was found!" | |
} | |
#Create a Lync connection for later use | |
$session = New-PSSession -ConnectionUri https://<YOUR.LYNC.WEB.LOCATION.HERE>/OcsPowershell -Authentication Negotiate | |
try | |
{ | |
if($action -eq "Add") | |
{ | |
#Double check to make sure they don't currently have a number assigned | |
$oldNumber = "" | |
#Connect to Lync | |
Import-PsSession $session | |
$csUser = Get-CSUser $Employee.SamAccountName | |
#End the Lync Connection | |
$oldNumber = $csUser.LineURI | |
if($oldNumber) | |
{ | |
Write-Output "The selected employee already has an assigned number." | |
} | |
else | |
{ | |
#Format the number for Lync as "tel:+11231234567" | |
$newLyncNumber = "tel:+1" + ($newNumber -replace '[-]','') | |
Set-CsUser -Identity $Employee.UserPrincipalName -LineUri $newLyncNumber -EnterpriseVoiceEnabled $True | |
} | |
Remove-PsSession $session | |
#If The user doesn't already have an assigned number, continue | |
if(!$oldNumber) | |
{ | |
#Set the Number in the AD field | |
Set-ADUser $Employee.SamAccountName -OfficePhone $newNumber | |
Write-Output "Number has been added successfully" | |
} | |
} | |
elseif($action -eq "Remove") | |
{ | |
$oldNumber = "" | |
#Connect to Lync | |
Import-PsSession $session | |
#Get the users current number | |
$csUser = Get-CSUser $Employee.SamAccountName | |
$oldNumber = $csUser.LineURI | |
#Remove the number in Lync | |
Set-CsUser -Identity $Employee.UserPrincipalName -LineUri $null -EnterpriseVoiceEnabled $False -AudioVideoDisabled $False -RemoteCallControlTelephonyEnabled $False | |
#End the Lync Connection: | |
Remove-PsSession $session | |
#Remove the number in AD | |
Set-ADUser $Employee.SamAccountName -OfficePhone $null | |
Write-Output "Number has been removed successfully" | |
} | |
elseif($action -eq "Change") | |
{ | |
#Format the number for Lync as "tel:+13061234567" | |
$newLyncNumber = "tel:+1" + ($newNumber -replace '[-]','') | |
$oldNumber = "" | |
#Connect to Lync | |
Import-PsSession $session | |
$csUser = Get-CSUser $Employee.SamAccountName | |
$oldNumber = $csUser.LineURI | |
#Set the Number in Lync | |
Set-CsUser -Identity $Employee.UserPrincipalName -LineUri $newLyncNumber -EnterpriseVoiceEnabled $True | |
#End the Lync Connection: | |
Remove-PsSession $session | |
#Remove the number in AD | |
Set-ADUser $Employee.SamAccountName -OfficePhone $newNumber | |
Write-Output "Number has been changed successfully" | |
} | |
} | |
catch | |
{ | |
Write-Output "$($_.Exception.Message) (At line $($_.Exception.Line), character $($_.Exception.Offset))" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment