get-msolipranges.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function Get-MSOLIPRanges {
    <#
    .SYNOPSIS
    Finds IP range for given O365 Products to create firewall rules.
    .DESCRIPTION
    Finds IP range for given O365 Products to create firewall rules.
    .PARAMETER IPType
    Select which IP type you want to find the ranges for.
    Possible options are IPv4 and IPv6, default is IPv4.
    .EXAMPLE
    Get-MSOLIPRanges
    #>

    [cmdletbinding()]
    param(
        [Parameter(Position = 0)]
        [ValidateSet('IPv4', 'IPv6')]
        [string]$IPType = 'IPv4'
    )
    $Office365IPsXml = New-Object System.Xml.XmlDocument
    $Office365IPsXml.Load("https://support.content.office.net/en-us/static/O365IPAddresses.xml")
    $updated = $Office365IPsXml.products.updated
    Write-Output "Last updated: $updated"
    [array]$Products = $Office365IPsXml.products.product | Sort-Object Name | Out-GridView -PassThru -Title 'Select Product(s)'
    foreach ($Product in $Products) {
        $IPRanges = ($Product | Select-Object -ExpandProperty Addresslist | Where-Object {$_.Type -eq $IPType}) | Where-Object {$_.address -ne $null} | Select-Object -ExpandProperty address
        foreach ($Range in $IPRanges) {
            $ProductIPRange = New-Object -TypeName PSObject -Property @{
                'Product' = $Product.Name;
                'IPRange' = $Range;
            }
            Write-Output $ProductIPRange | Select-Object Product, IPRange
        }
    }
}