Get Lync Assigned Numbers v4.1 (Improved)

I’ve made some improvements to the original script that gathers all actively used numbers based on a Site ID posted to this page.  To summarize it pulls the LineURI from any Lync account type that can hold a phone number and exports them all to a CSV.  From here you can sort by their number and determine what phone numbers are currently available.  The account types that are included in the search are:

CSUser (LineURI and Private Line)
CSCommonAreaPhone
CSAnalogDevice
CSRGSWorkFlow
CSEXUMContact
CSDialInConferencingAccessNumber
CSTrustedApplicationEndpoint
CSMeetingRoom

 The most desired change is using FILTER instead of Where-Object when applicable.  This change has dropped the run time from over 5+ minutes to roughly 20-30 seconds within an environment that holds over 115,000 Lync accounts.

Additionally, the powershell window in which you run this script from will display the unassigned number ranges within that site.  It also searches for Private Lines and Meeting rooms.

#Version 4.1 of this script. Much nicer than the first, second, third and first fourth.
#Created by Jeff McBride, www.lynclead.com
#Created on 12/23/2014
#Primary upgrade is utilizing FILTER instead of WHERE where applicable. 
#This speeds up the run time dramatically in enterprise size environments
$SiteID = Read-Host "What is the site code for the number range you want to look up?"
$EnteredSite = Get-CsUnassignedNumber | Where-Object {$_.identity -match $SiteID}
$starttime = get-date
Write-Host "This script may take several minutes depending on the site."
Write-Host ""
Write-Host "The following are the DID range(s) currently being scanned and exported for active numbers"
Write-Host ""
foreach ($RangeListing in ($EnteredSite))
 {
 $BeginRange = $RangeListing.NumberRangestart.TrimStart("tel:") 
 $EndRange = $RangeListing.NumberRangeEnd.TrimStart("tel:")
$BeginRangeTel = "tel:$BeginRange"
 $EndRangeTel = "tel:$EndRange"
Write-Host "Start Range:" $RangeListing.NumberRangestart
 Write-Host "End Range: " $RangeListing.NumberRangeEnd
 Write-Host ""
Get-CSUser -Filter {LineURI -ge $BeginRangeTel -and LineURI -le $EndRangeTel} | Select SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 Get-CSUser -Filter {PrivateLine -ge $BeginRangeTel -and PrivateLine -le $EndRangeTel} | Select SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 Get-CSCommonAreaPhone -Filter {DisplayNumber -ge $BeginRange -and DisplayNumber -le $EndRange} | Select-Object SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 Get-CSAnalogDevice -Filter {DisplayNumber -ge $BeginRange -and DisplayNumber -le $EndRange} | Select SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 Get-CSRGSWorkflow | Where-Object {$_.LineURI -ge "$BeginRangeTel" -and $_.LineURI -le "$EndRangeTel"} | Select SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 Get-CSEXUMContact -Filter {DisplayNumber -ge $BeginRange -and DisplayNumber -le $EndRange} | Select SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 Get-CsDialInConferencingAccessNumber -Filter {LineURI -ge $BeginRangeTel -and LineURI -le $EndRangeTel} | Select SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 Get-CsTrustedApplicationEndpoint | Where-Object {$_.DisplayNumber -ge "+$BeginRange" -and $_.DisplayNumber -le "+$EndRange"} | Select SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 Get-CSMeetingRoom | ? {$_.LineURI -ge "$BeginRangeTel" -and $_.LineURI -le "$EndRangeTel"} | Select SipAddress, DisplayName, LineURI | Export-CSV -append -NoTypeInformation (".\" + $SiteID + "_AssignedNumbers.csv")
 }
Write-Host "OK, I finished. Have a nice day."
$endtime = get-date
$timeoutput = ($endtime - $starttime)
write-host "Time to run this script: $timeoutput"

Leave a Reply