Published on

Using the Office 365 IP Address and URL web service in PowerShell

Authors
  • avatar
    Name
    Jonathan Devere-Ellery
    Twitter

The Office 365 IP Address and URL web service is a pretty useful way of manipulating the IP addresses or hostnames that are needed to connect successfully to your Office 365 tenant.

The full list of hostnames and IPs are on Docs, but sometimes having this in PowerShell makes it easier to query and manipulate.

To lift the description of these endpoint categories directly from the public documentation, these categories can be described as:

  • Optimize are "the most sensitive to network performance, latency, and availability." These should be top of our list to exclude from Proxies/TLS termination/Web Filtering policies etc.
  • Required are "required for connectivity to specific Office 365 services and features, but are not as sensitive to network performance and latency as those in the Optimize category."
  • Default "do not require any optimization, and can be treated by customer networks as normal Internet bound traffic."

First we need to generate a GUID that we need to pass along with our requests. This would typically done by running $guid = New-Guid but if you have an older v3/v4 version of PowerShell you can run $guid = [guid]::NewGuid()

Next we query the endpoint and store it in a variable which we can query and manipulate later

$ep = Invoke-RestMethod("https://endpoints.office.com/endpoints/worldwide?clientrequestid=$guid&NoIPv6=true") # IPv4 only

Or we can include the TenantName variable, and the results we get back for SharePoint Online etc will include the tenant-specific URLs for us. If we leave this blank then the results will be generic, and would need to be customised later.

$ep = Invoke-RestMethod("https://endpoints.office.com/endpoints/worldwide?clientrequestid=$guid&TenantName=Contoso&NoIPv6=true") 

Then it becomes extremely simple to list out domains or IPs that we are interested in, such as some of the examples below:

$ep | Where-Object {$_.Category -eq "Optimize" -And $_.ServiceArea -eq "Exchange"} | Format-Table -autosize

$ep | Where-Object {$_.ServiceArea -eq "Common" -And $Null -ne $_.Ips} | Select-Object -ExpandProperty Ips -Unique

$ep | Where-Object {$_.tcpPorts -Like "25"}

$ep | Where-Object {$_.Category -eq "Optimize"} | Format-Table serviceArea,urls