Skip to content

Instantly share code, notes, and snippets.

@theagreeablecow
Created June 6, 2012 05:09
Show Gist options
  • Save theagreeablecow/2880040 to your computer and use it in GitHub Desktop.
Save theagreeablecow/2880040 to your computer and use it in GitHub Desktop.
Get Unassigned Numbers in Lync 2010
<#
.SYNOPSIS
Find assigned or unassigned numbers in Lync
.DESCRIPTION
This script uses uses the 'Unassigned Number' ranges in Lync to look number usage, including:
- User numbers (including Private Number)
- Analogue Devices
- Common Area Phones
- AutoAttendant numbers
- Dial In Access Numbers
- Trusted Application Endpoints
- Response Groups
.PARAMETER FileName
<none>
.EXAMPLE
.\Lync_GetUnassignedNumbers.ps1
.NOTES
ScriptName: Lync_GetUnassignedNumbers.ps1
Created By: Ståle Hansen
Updated By: TheAgreeableCow
Date Coded: June 2012
.LINK
http://msunified.net/lyncdownloads/script-list-unusednumbers-ps1/
http://theagreeablecow.com/
#>
#LOAD POWERSHELL SESSIONS
#------------------------
$Lyncserver = "lync1.mydomain.com.au"
$usercredential= get-credential -credential mydomain.com.au\administrator
cls
write-host -foregroundcolor Green "Loading modules for Lync..."
$lyncsession = new-pssession -connectionuri https://$Lyncserver/ocspowershell -credential $usercredential
Import-PSSession $lyncsession
#LOAD UNASSIGNED NUMBER RANGES
#-----------------------------
[System.Console]::ForegroundColor = [System.ConsoleColor]::White
clear-host
Write-Host "Script for finding unassigned numbers in Lync Server 2010."
Write-Host
foreach ($Serie in (Get-CsUnassignedNumber))
{
#VARIABLES
#----------
#Country Code
$CountryCodeLength=2
#The "tel:+" string is the +5 lenght that is added in the next line
$CountryCodeLength=$CountryCodeLength+5
#Replace string so that all numbers can be converted to an int
$ReplaceValue=($Serie.NumberRangeStart).Substring(0,$CountryCodeLength)
#Check to see if Unassigned Numbers are in E.164 format, if its not, continue to the next number serie
if (($ReplaceValue.Substring(0,5)) -ne "tel:+")
{
Write-Host "The script requires that Unassigned Numbers are populated in E.164 format" -Foregroundcolor Yellow
Write-Host "It appears that the number range " -nonewline
Write-Host $Serie.Identity -nonewline -Foregroundcolor Green
Write-Host " is not in this format"
Write-Host
Continue
}
$NumberStart=$Serie.NumberRangeStart | ForEach-Object {$_.Replace($ReplaceValue, "")}
$NumberEnd=$Serie.NumberRangeEnd | ForEach-Object {$_.Replace($ReplaceValue, "")}
$Ser=$NumberStart..$NumberEnd
$ErrorActionPreference = 'SilentlyContinue'
#GET USED NUMBERS
#-----------------
$Used=Get-CsUser -Filter {LineURI -ne $Null} | Select-Object LineURI | out-string -stream
$Used+=Get-CsUser -Filter {PrivateLine -ne $Null} | Select-Object PrivateLine | out-string -stream
$Used+=Get-CsAnalogDevice -Filter {LineURI -ne $Null} | Select-Object LineURI | out-string -stream
$Used+=Get-CsCommonAreaPhone -Filter {LineURI -ne $Null} | Select-Object LineURI | out-string -stream
$Used+=Get-CsExUmContact -Filter {LineURI -ne $Null} | Select-Object LineURI | out-string -stream
$Used+=Get-CsDialInConferencingAccessNumber -Filter {LineURI -ne $Null} | Select-Object LineURI | out-string -stream
$Used+=Get-CsTrustedApplicationEndpoint -Filter {LineURI -ne $Null} | Select-Object LineURI | out-string -stream
$Used+=Get-CsRgsWorkflow | Select-Object LineURI | out-string -stream
$Used=$Used | ForEach-Object {$_.ToLower()}
$Used=$Used | ForEach-Object {$_.Replace($ReplaceValue, "")}
$Used=$Used | ForEach-Object {$_.split(';')[0]}
$ErrorActionPreference = 'Continue'
$AllUsed=@()
foreach($Series in $Ser)
{foreach($UsedNumber in $Used)
{
if($Series -eq $UsedNumber){$AllUsed+=$UsedNumber}
}
}
$ListUnUsed=@()
$ComparisonResult=compare-object $Ser $AllUsed
foreach($UnUsed in $ComparisonResult)
{
if($UnUsed.SideIndicator -eq '<='){$ListUnUsed+=$UnUsed.InputObject;$FreeSize++}
}
$RangeSize=($NumberEnd-$NumberStart)+1
$TotalUsed = $RangeSize-$FreeSize
$TotalFree = $RangeSize-$TotalUsed
#OUTPUT RESULTS
#--------------
Write-Host "In the range " -nonewline
Write-Host $Serie.Identity -NoNewLine -Foregroundcolor Green
Write-Host ", there are " -NoNewLine
Write-Host $TotalFree -NoNewLine
Write-Host " of"$RangeSize "free."
Write-Host "This range starts at " -NoNewLine
Write-Host $NumberStart -NoNewLine -Foregroundcolor Green
Write-Host " and ends at " -NoNewLine
Write-Host $NumberEnd -Foregroundcolor Green
Write-Host "Press "-NoNewLine
Write-Host "U" -NoNewLine -Foregroundcolor Green
Write-Host " for Unassigned numbers or "-NoNewLine
Write-Host "A" -NoNewLine -Foregroundcolor Green
Write-Host " for Assigned numbers"-NoNewLine
$opt = Read-Host " (Enter to skip)"
if($opt -eq "U"){$ListUnUsed}
if($opt -eq "A"){$AllUsed}
Write-Host
$FreeSize=0
$ListUnUsed=$NULL
$UsedNumbers=$NULL
}
[System.Console]::ForegroundColor = [System.ConsoleColor]::Gray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment