Script to find available telephone numbers in Lync Server 2010

Having worked on a couple of Enterprise Voice deployments since the launch of Lync Server 2010 I have been missing a feature that I honestly thought would be incorporated by launch. It is the ability to find unused available numbers in a given number range assigned to the Lync Server based on Unassigned Numbers. I even hoped it would be possible to assign a teluri to users through a drop down list in the Silverlight GUI since the information is available in the server. I hope this will be available in a later Service Pack or Cumulative Update.

Since this is a feature I really want, I have tried to create a PowerShell Script to find out how many numbers are available in any given number range and what they are. First of all I want to declare that I am a PowerShell novice so my syntax may not be that optimized. The script works, though there’s a lot of variables and loops that I am sure could be done better. The script does the following:

  • It goes through each entry in Unassigned Phone Numbers that needs to be populated with all numbers series available to the Lync Server
  • Then it gets all teluri’s for all the users, dial-in conferencing and Exchange UM contacts
    • I didn’t find the response group command to get the teluri while I was at it, will add this in an update
  • Then the string gets “washed” for “tel:+47” where 47 is the hardcoded region code for Norway
    • The string also get “washed” for any extension numbers
  • After that a new string gets created to get rid of property types and so on
  • I use Compare-Object to compare the Unassigned Number array with the used numbers
    • Run a check to find what numbers where not used and count them
  • Then I find out what the total number of unused numbers are and write the output

I hope this script will come useful to anyone who needs this feature and that others can use this to and adapt it to their needs. Please share any improvements that could be made to the script. Enjoy!

https://msunified.net/lyncdownloads/script-list-unusednumbers-ps1/

15 thoughts on “Script to find available telephone numbers in Lync Server 2010

  1. Hi, use Get-CsRgsWorkflow | select name, lineuri to find the numbers for the response groups.

  2. Thank you for the script this will help us alot with cleaning up our current DID lists and consolodating numbers.
    I have run into a problem and it might just be with how I have done things.
    We have two sites with 780-xxx-xxxx numbers and 403-xxx-xxxx I have entered the unused number entries as 403xxxxxx to 403xxxxxxx and when I run the script I get back and error, please see below.
    If I modify the replace lines and remote the 780 or the 403 it works okay but then the script only works for one of my two sites, I hope this is somewhat clear. Let me know if you have any thoughts.

    Thanks.

    Script for finding unused numbers in Lync Server 2010, by Ståle Hansen
    WARNING: The script requires that Unassigned Numbers are populated

    Cannot convert value “4032124375” to type “System.Int32”. Error: “Value was eit
    her too large or too small for an Int32.”
    At C:\Temp\scripts\List-UnsuedNumbersEdmonton.ps1:18 char:20
    + $Ser=$numberstart.. <<<< $numberend
    + CategoryInfo : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

    • Hi Jesse. Is it possible that your unassigned number range is in not it E.164 format? Maybe you are missing the + sign in front. It looks like the script cant use your range as int and therefore cant calculate all the numbers between numberrange start and numberrange end. Also you need to change all the {$_.Replace(“tel:+47”, “”)} in the script to your countrycode, like +47 is for Norway. That is what I can come up with given your information.

      • So in the error above it says that it can not convert 4032124375 to an Integer but if I change my replace {$.Replace rule from {$.Replace(“tel:+1″,””)} to {$.Replace(“tel:+1403″,””)} it will run for all of my Calgary numbers but Fail on my Edmonton numbers and if I set {$.Replace(“tel:+1780″,””)} it will run for my Edmonton numbers but fail on my Calgary numbers.
        All numbers are set as tel:+1403723XXXX in the unassigned numbers area of Lync.

        • Ok, then you probably need to add some extra lines to remove both the tel:+1403 and tel:+1780. Your problem gave me an idea to change the script to be more dynamic using variables instead of hardcoded values. I will se if I can do this as soon as I find time.

    • Thats perfect, I tested it out and it worked great, I had to do some copy paste work from the original to the new one you posted as line 70 had some missing code from the old version.
      Thank you very much.

  3. How would I use this same script to export these to a file so we would have a list that could be updated with unused numbers?

  4. Awesome script Ståle! We tend not to use the Unassigned Numbers list here in Australia. It costs nothing to receive a supervisory message from the carrier if you’ve dialled a vacant number, and many of our larger customers really aren’t wanting to bother their reception (or UM) with calls to vacant numbers.

    With that in mind, I’ve added a front-end to your script that replaces the Unassigned numbers list, giving the customer a menu of available ranges to choose from.

    All the customer needs to do is hard-code their number ranges into the top of the script once, and away they go.

    I’ve used the following code (which you can edit or amend freely of course) to replace from the top of your original script down to and including the line “foreach ($Serie in (get-csUnassignedNumber)) {”

    ####################################################################################################
    # List-UnusedNumbers.ps1
    #
    # v10 April 2011 by Ståle Hansen, Lync MVP (http://msunified.net)
    #
    # Thanks to Marjus Sirvinsks (http://marjuss.wordpress.com) for pointing me in the right direction
    #
    ####################################################################################################
    [System.Console]::ForegroundColor = [System.ConsoleColor]::White
    clear-host

    Write-Host
    Write-Host “Script for finding unused numbers in Lync Server 2010, by Ståle Hansen”
    Write-Host “- With menu front-end by Greig Sheridan, 2012. https://greiginsydney.com
    Write-Host
    Write-Host

    $Ranges = @()

    #Copy and repeat the 5 lines below for each number range you have in use:
    #—————————————————————————
    $NumberRange = New-Object Object
    $NumberRange | Add-Member -Type NoteProperty -name “Identity” -value “Sydney”
    $NumberRange | Add-Member -Type NoteProperty -name “NumberRangeStart” -value “tel:+61281234500”
    $NumberRange | Add-Member -Type NoteProperty -name “NumberRangeEnd” -value “tel:+61281234599”
    $Ranges += $NumberRange
    #—————————————————————————

    $NumberRange = New-Object Object
    $NumberRange | Add-Member -Type NoteProperty -name “Identity” -value “Melbourne”
    $NumberRange | Add-Member -Type NoteProperty -name “NumberRangeStart” -value “tel:+61370001200”
    $NumberRange | Add-Member -Type NoteProperty -name “NumberRangeEnd” -value “tel:+61370001299″
    $Ranges += $NumberRange

    #First figure out the maximum width of the site’s name (for the tabular menu):
    $width=0
    foreach ($Serie in ($Ranges)) {
    if ($Serie.Identity.Length -gt $width) {
    $width = $Serie.Identity.Length
    }
    }

    #Provide an on-screen menu of indial ranges for the user to choose from:
    $index = 0
    foreach ($Serie in ($Ranges)) {
    write-host ($index.ToString()).PadRight(3,” “), $Serie.Identity.Padright($width+1,” “), $Serie.NumberRangeStart, $Serie.NumberRangeEnd
    $index++
    }
    $index– #Undo that last increment
    Write-Host
    write-host “Choose the range you’re interested in”
    $chosen = read-host “Or any other value to quit: ”

    if ($chosen -notmatch ‘^\d$’) {Exit}
    if ([int]$chosen -lt 0) {Exit}
    if ([int]$chosen -gt $index) {Exit}

    Write-Host
    Write-Host “Wait while I search the entire database. This could take a while…”
    Write-Host

    $Selected = $Ranges[$chosen]

    foreach ($Serie in ($Selected)) {

    ———————————————-

    The on-screen output looks like this (for the above code, with only 2 sites / 2x hundred-ranges entered):

    Script for finding unused numbers in Lync Server 2010, by Ståle Hansen
    – With menu front-end by Greig Sheridan, 2012. https://greiginsydney.com

    0 Sydney tel:+61281234500 tel:+61281234599
    1 Melbourne tel:+61370001200 tel:+61370001299

    Choose which range you’re interested in
    Or any other value to quit: : 1

    Wait while I search the entire database. This could take a while…

    Total free numbers in number range Melbourne, 100 of 100
    This range start with 370001200 and ends with 370001299
    To list available numbers, press L else press Enter:

  5. […] Ståle Hansen has published a great PowerShell script that digs through the Lync CsUnassignedNumber list and mashes it against all of the possible uses of a number (including CsAnalogDevices, CommonAreaPhones, RGS, UMContacts and more) to report what numbers in a given number range are currently spare. It fills a yawning (and frustrating gap) in the Lync administrator’s toolkit. […]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s