Obtain and Apply Phone Numbers Via PowerShell

This page is an example of how to obtain new DID phone numbers directly from Microsoft, and then apply them to existing Office 365 users that already have the necessary Cloud PBX licenses enabled…

Before proceeding in PowerShell,  it’s a good idea to review Microsoft’s steps for performing this process from within the web browser. They are as follows…

Step 1: Buy and assign licenses
Step 2: Get phone numbers
Step 3: Add emergency addresses and locations for your organization
Step 4: Assign phone numbers and emergency addresses to users
Step 5: Tell your users about their new phone numbers

Here is how I’m getting this done for my own user account on Office 365…

Getting connected…

To work with the Skype for Business Online commands in PowerShell, you’ll need to have the Skype for Business module installed. You can obtain this at the following URL, and then importing the LyncOnlineConnector module in your PowerShell session

# once installed, you can import the SfB module and then get authenticated using it...
Import-Module LyncOnlineConnector
$SfBSession = New-CsOnlineSession
Import-PSSession $SfBSession

Configuring LIS (location information services) for emergency services…

This is shown as step 4, but I prefer to get it out of the way for use later on since it doesn’t require any of the phone # or user details to be completed. Below is an example of creating a single “civic address” which also acts as a “location”…

$houseNumber = '310'
$streetName = 'West 51th'
$streetSuffix = 'St'
$postDirectional = ' '
$city = 'New York'
$stateOrProvince = 'NY'
$country = 'US'
$postalCode = '10019'
$description = 'NYC Home'
$companyName = 'Home'

# create a new civic address in the system...
$newLisCivicAddress = New-CsOnlineLisCivicAddress -HouseNumber "$houseNumber" `
 -StreetName "$streetName" -StreetSuffix "$StreetSuffix" `
 -PostDirectional "$postDirectional" -City "$city" `
 -StateOrProvince "$stateOrProvince" -Country "$country" -PostalCode "$postalCode" `
 -Description "$description" -CompanyName "$companyName"

# validate and save the newly created civic address...
Test-CsOnlineLisCivicAddress -CivicAddressId $newLisCivicAddress.CivicAddressId

# store the new location's ID for user later on...
$LisLocationId = (
Get-CsOnlineLisLocation-CivicAddressId $newLisCivicAddress.CivicAddressId

# or to find one by street name...
# $lisLocations = Get-CsOnlineLisLocation
# $LisLocationId = ($lisLocations | Where {$_.StreetName -eq 'WALL ST'}).LocationId

Getting phone numbers…

Before you can get a list of phone numbers and then request which ones you want to link to your subscription, you may want to browse them. For my example, I’ll focus on getting a number in the 917 area code which is one of the NYC area codes. To do that I needed to know a few things…

  • region
  • country
  • area
  • city
  • type of # you want (Subscriber for users, Service for things like conferencing)

I’m using the following values and commands to temporarily reserve a block of numbers to be reviewed and then later formally taken out of inventory. One note, in the GUI my account is limited to return 10 or less numbers per search. But in PowerShell I’m finding I can search for much more at a time to get a better selection…

# my hard-coded location settings. scroll down for examples on finding your own...
$thisRegion = 'NOAM'
$thisCountry = 'US'
$thisArea = 'NY'
$thisCity = 'NY'
$thisAreaCode = '917'

$search = Search-CsOnlineTelephoneNumberInventory -InventoryType Subscriber `
 -Region NOAM -Country US -Area $thisArea -CapitalOrMajorCity $thisCity `
 -AreaCode $thisAreaCode -Quantity 100

Assigning Phone Numbers…

Once I am able to take a look at the numbers that are reserved, I do the following to assign one to my subscription for use later on…

$searchId = $search.ReservationId
$thisNumber = $search.Reservations[0].Numbers | Where {$_.Number -Like '*9178190901*'}
Select-CsOnlineTelephoneNumberInventory -ReservationId $searchId `
 -TelephoneNumbers $thisNumber.Number -Region "$thisRegion" `
 -Country "$thisCountry" -Area "$thisArea" -City "$thisCity"

Next,  I can finally take the previously defined LIS location, the chosen phone #, and apply them both to a specific user…

# choose the user that you will assign a number to...
$voiceUsers = Get-CsOnlineVoiceUser
$voiceUsers | fl
$thisUser = $voiceUsers | Where {$_.Name -eq 'Greg Besso'}

# choose one of your spare phone #'s...
$spareNumbers = Get-CsOnlineTelephoneNumber -IsNotAssigned
$thisNumber = $spareNumbers[0]
# OR to choose a specific one: $thisNumber = $spareNumbers | Where {$_.Id -Like '*6009*'}
$thisNumber = "+" + $thisNumber.Id

# assign the chosen LIS location and phone # to the chosen user...
Set-CsOnlineVoiceUser -Identity $thisUser.Id `
-TelephoneNumber $thisNumber `
-LocationID $LisLocationId

And that’s it. At this point the selected user is now ready to make and receive phone calls from their Office 365 Skype for Business account.

*If you want to enumerate all the available regions, countries, areas/states, cities, etc… and then choose your own, do something like the following…

# if you want to find a region to choose...
$getRegions = Get-CsOnlineTelephoneNumberInventoryRegions -InventoryType Subscriber
$getRegions = $getRegions | Sort-Object -Property DefaultName
$thisRegion = 'NOAM' # or whichever ID you prefer from the list above

# if you want to find a country to choose...
$countries = Get-CsOnlineTelephoneNumberInventoryCountries -RegionalGroup $thisRegion `
-InventoryType Subscriber
$countries = $countries | Sort-Object -Property DefaultName
# $countries
$thisCountry = ($countries | Where {$_.Id -eq 'US'}).Id # or whichever ID you choose

# if you want to choose an area/state...
$areas = Get-CsOnlineTelephoneNumberInventoryAreas -RegionalGroup NOAM `
 -CountryOrRegion US -InventoryType Subscriber
$areas = $areas | Sort-Object -Property DefaultName
# $areas
$thisArea = ($areas | Where {$_.Id -eq 'NJ'}).Id

# if you want to choose a city and area code...
$cities = Get-CsOnlineTelephoneNumberInventoryCities -InventoryType 'Subscriber' `
 -RegionalGroup NOAM -CountryOrRegion US -Area $thisArea.Id
$cities = $cities | Sort-Object -Property DefaultName
# $cities
$thisCity = $thisCities | Where{$_.Id -eq 'NY'}
# view the city information, then choose an area code and assign the ID once done...
$thisCity = $thisCity.Id
$thisAreaCode = '917'



References for more information…

Skype for Business Online cmdlets (TechNet)


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