Skip to content

Instantly share code, notes, and snippets.

@miracles1315
Last active September 9, 2017 05:10
Show Gist options
  • Save miracles1315/41cbcbe2018e1399f09029bee583c634 to your computer and use it in GitHub Desktop.
Save miracles1315/41cbcbe2018e1399f09029bee583c634 to your computer and use it in GitHub Desktop.
The function gets the Office 365 IP address & URL information, in XML format, and returns the Product (O365, SPO, etc...), AddressType (IPV4,IPV6, OR URL), and Address (Ex: 13.107.6.152/31) information.
Function Get-O365IPAddress
{
<#
.SYNOPSIS
The function gets the Office 365 IP address & URL information.
.DESCRIPTION
The function gets the Office 365 IP address & URL information, in XML format, and returns the Product (O365, SPO, etc...), AddressType (IPV4,IPV6, OR URL), and Address (Ex: 13.107.6.152/31) information.
.PARAMETER ErrorLog
The file to log script errors to. This parameter is not required. Default file is C:\GetO365IPAddress-ErrorLog.txt. In order for errors to be logged out to the file, the -LogErrors parameter must be used.
Example Paramter Input:
1. File name only : Errors.txt (Note: File is created in the current directory.)
2. Using relative path: .\Errors.txt (Note: File is created in the current directory.)
3. Using full path : C:\Errors.txt
4. Using UNC path : \\Server1\Errors\Errors.txt
.PARAMETER LogErrors
In order for script errors to be logged to the error file, this switch parameter must be used. By default errors are not logged to the file. This parameter is not required.
.EXAMPLE
Get-O365IPAddress
.EXAMPLE
Get-O365IPAddress -LogErrors -ErrorLog C:\GetO365IPAddress-ErrorLog.txt
.INPUTS
Office 365 IP address & URL information from https://support.content.office.net/en-us/static/O365IPAddresses.xml.
.OUTPUTS
Office 365 IP address & URL information, including Product (O365, EXO, SPO, etc...), AddressType (IPV4, IPV6, OR URL), and Address (Ex: 13.107.6.152/31).
.NOTES
Last Updated: 9/8/17
.LINK
Invoke-WebRequest
https://support.office.com/en-us/article/Office-365-URLs-and-IP-address-ranges-8548a211-3fe7-47cb-abb1-355ea5aa88a2
https://support.content.office.net/en-us/static/O365IPAddresses.xml
#>
[CmdletBinding()]
Param
(
[Switch] $LogErrors,
[ValidateNotNullOrEmpty()]
[String] $ErrorLog = 'C:\GetO365IPAddress-ErrorLog.txt'
)
Try
{
Write-Verbose "Getting the Office 365 IP address & URL information from $O365IPAddresses."
$O365IPAddresses = "https://support.content.office.net/en-us/static/O365IPAddresses.xml"
[XML]$O365XMLData = Invoke-WebRequest -Uri $O365IPAddresses -DisableKeepAlive -ErrorAction Stop
Write-Debug "Got the the Office 365 IP address information from $O365IPAddresses."
} #End 'Try' block
Catch
{
If($LogErrors)
{
Write-Verbose "Failed to get the Office 365 IP address & URL information from $O365IPAddresses. See $ErrorLog for further information."
$ErrorMsg = "$(Get-Date): [ERROR]: Failed to get the Office 365 IP address & URL information from $O365IPAddresses. Error message was: $($_.Exception.Message)"
$ErrorMsg | Out-File -FilePath $ErrorLog -Append
} #End 'If' block
Else
{
Write-Verbose "Failed to get the Office 365 IP address & URL information from $O365IPAddresses. To log the error details, re-run Get-O365IPAddress with the -LogErrors parameter."
} #End 'Else' block
} #End 'Catch' block
If($O365IPAddresses)
{
ForEach($Product in $O365XMLData.Products.Product)
{
ForEach($AddressList in $Product.AddressList)
{
$O365IPAddressProps = @{
Product = $Product.Name;
AddressType = $AddressList.Type;
Addresses = $AddressList.Address
}
$O365IPAddressObj = New-Object -TypeName PSObject -Property $O365IPAddressProps
Write-Output $O365IPAddressObj
} #End 'ForEach' block
} #End 'ForEach' block
} #End 'If' block
} #End 'Get-O365IPAddress' function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment