Function Get-O365Endpoints { <# .Synopsis Powershell Method for pulling updated list of IP ranges for Office 365 endpoints from Microsoft's published xml file. Initial reference came from existing script on TechNet available here: https://gallery.technet.microsoft.com/Get-Office-365-IP-v4-562987d5 .Description Explanation: https://support.office.com/en-us/article/Office-365-URLs-and-IP-address-ranges-8548a211-3fe7-47cb-abb1-355ea5aa88a2 XML file: https://support.content.office.net/en-us/static/O365IPAddresses.xml .Parameter Products One or more Office 365 products by their abbreviation in the xml file: EOP, EXO, Identity, LYO, o365, OneNote, Planner, ProPlus, RCA, SPO, Sway, WAC, Yammer. Note: Parameter will need to be maintained as products are added and removed by Microsoft, at which point the parameter should be updated to match the current list of products in the xml file. .Parameter AddressType The desired information regarding the product: IPv4, IPv6, or URL. .Example Get-O365EndPoints -Products LYO -AddressType IPv4 .Example Get-O365EndPoints -Products LYO -AddressType URL .Example Get-O365EndPoints -Products LYO -AddressType IPv6 | Export-Csv Office365IPRanges.csv -NoTypeInformation #> [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] [ValidateSet("o365","LYO","Planner","Teams","ProPlus","OneNote","Yammer","EXO","Identity","Office365Video","WAC","SPO","RCA","Sway","EX-Fed","OfficeMobile","CRLs","OfficeiPad","EOP")] [string[]]$Products = @(), [Parameter(Mandatory = $false)] [ValidateSet("IPv4","IPv6","URL")] [string[]]$AddressType = @() ) Begin{ try { $Office365IPsXml = New-Object System.Xml.XmlDocument $Office365IPsXml.Load("https://support.content.office.net/en-us/static/O365IPAddresses.xml") } catch { Write-Warning -Message "Failed to load xml file https://support.content.office.net/en-us/static/O365IPAddresses.xml" break } if (-not ($Office365IPsXml.ChildNodes.NextSibling)) { Write-Warning -Message "Data from xml is either missing or not in the expected format." break } } Process{ foreach ($Product in ($Office365IPsXml.products.product | Where-Object ({$Products -match $_.Name}) | Sort-Object Name)) {If($AddressType -contains "IPv4") { $IPv4Ranges = $Product | Select-Object -ExpandProperty Addresslist | Where-Object {$_.Type -match "IPv4"} $IPv4Ranges = $IPv4Ranges | Where-Object {$_.address -ne $null} | Select-Object -ExpandProperty address foreach ($Range in $IPv4Ranges) { $ProductIPv4Range = New-Object -TypeName psobject -Property @{ 'Product'=$Product.Name; 'IPv4Range'=$Range; } Write-Output $ProductIPv4Range | Select-Object Product, IPv4Range } } ElseIf($AddressType -contains "IPv6") { $IPv6Ranges = $Product | Select-Object -ExpandProperty Addresslist | Where-Object {$_.Type -match "IPv6"} $IPv6Ranges = $IPv6Ranges | Where-Object {$_.address -ne $null} | Select-Object -ExpandProperty address foreach ($Range in $IPv6Ranges) { $ProductIPv6Range = New-Object -TypeName psobject -Property @{ 'Product'=$Product.Name; 'IPv6Range'=$Range; } Write-Output $ProductIPv6Range | Select-Object Product, IPv6Range } } ElseIf($AddressType -contains "URL") { $URLRanges = $Product | Select-Object -ExpandProperty Addresslist | Where-Object {$_.Type -match "URL"} $URLRanges = $URLRanges | Where-Object {$_.address -ne $null} | Select-Object -ExpandProperty address foreach ($Range in $URLRanges) { $ProductURLRange = New-Object -TypeName psobject -Property @{ 'Product'=$Product.Name; 'URLRange'=$Range; } Write-Output $ProductURLRange | Select-Object Product, URLRange } } Else { $AllRanges = $Product | Select-Object -ExpandProperty Addresslist $AllRanges = $AllRanges | Where-Object {$_.address -ne $null} | Select-Object -ExpandProperty address foreach ($Range in $AllRanges) { $AllProductRange = New-Object -TypeName psobject -Property @{ 'Product'=$Product.Name; 'Address'=$Range; } Write-Output $AllProductRange | Select-Object Product, Address } } } } }