Thursday, May 2, 2024
HomePowershellPowerShell is enjoyable :) Get all Microsoft IP and FQDNs for his...

PowerShell is enjoyable :) Get all Microsoft IP and FQDNs for his or her companies


When you find yourself in an atmosphere with strict web entry, you might want to determine what to open to what vacation spot for Microsoft companies like Change On-line, Groups, Endpoint Supervisor, and so forth. Microsoft publishes this on their web site however within the weblog publish, I want to present you a pleasant method of outputting that data in an Out-GridView or a CSV file.

How the script works

It downloads the knowledge from a Microsoft hyperlink that Microsoft maintains that accommodates all IP addresses and FQDNs with their TCP or UDP ports, after downloading it converts it from JSON to an object. Afterward, it shows the knowledge in an Out-GridView Home windows for simple sorting and filtering (You possibly can copy your choice within the window with CTRL-C) or to a CSV if you happen to use the CSVPath parameter.

Notice: The URL would possibly change and if it does… The script will present an error and what hyperlink you must edit within the script if wanted.

Operating the script

When operating it with none parameters, it appears to be like like this:

When operating it with the -CSVPath parameter and specifying the output path, instance under which saves it to d:tempendpoint.csv, it appears to be like like this:

Get-MicrosoftEndpoints -CSVPath d:tempendpoints.csv

The script

Under is the script, you’ll be able to add it to your PowerShell profile by:

notepad $profile
add '. c:scriptsget-microsoftendpoints.csv'
stop/save and begin a brand new PowerShell session
perform Get-MicrosoftEndpoints {
    param (      
        [parameter(parameterSetName = "CSV")][string]$CSVPath
    )
    
    #Disguise obtain progress, retrieve all Endpoints and Convert it from JSON format
    $ProgressPreference = "SilentlyContinue"
    strive  ConvertFrom-Json
        Write-Host ("Downloading worldwide Microsoft Endpoints") -ForegroundColor Inexperienced
    
    catch {
        Write-Warning ("Error downloading worldwide Microsoft Endpoints, please examine URL in script if it nonetheless matches") 
        Write-Warning ("the one discovered on this hyperlink https://be taught.microsoft.com/en-us/microsoft-365/enterprise/urls-and-ip-address-ranges?view=o365-worldwidein from the JSON formatted hyperlink ") 
    }
    
    $Whole = @()
    Write-Host ("Processing gadgets...") -ForegroundColor Inexperienced
    foreach ($Endpoint in $Endpoints) {
        #Test if IPs can be found for the Endpoint, set to False if not relevant
        if ($null -eq $Endpoint.ips) {
            $IPaddresses="Not relevant"
        }
        else {
            $IPaddresses = "$($Endpoint.ips)" -join ', '
        }

        #Test if TCP ports can be found for the Endpoint, set to False if not relevant
        if ($Endpoint.tcpPorts.Size -eq 0) {
            $TCPPorts="Not relevant"
        }
        else {
            $TCPPorts = $Endpoint.TCPPorts.Cut up(',') -join ', '
        }
            
        #Test if UDP ports can be found for the Endpoint, set to False if not relevant
        if ($Endpoint.udpPorts.size -eq 0) {
            $UDPPorts="Not relevant"
        }
        else {
            $UDPPorts = $Endpoint.udpPorts.Cut up(',') -join ', '
        }

        #Test if URLs can be found for the Endpoint, set to False if not relevant
        if ($Endpoint.urls.size -eq 0) {
            $URLlist="Not relevant"
        }
        else {
            $URLlist = $Endpoint.urls -join ', '
        }
                        
        $Merchandise = [PSCustomObject]@{
            serviceArea            = $Endpoint.serviceArea
            serviceAreaDisplayName = $Endpoint.serviceAreaDisplayName
            urls                   = $URLlist
            ips                    = $IPaddresses
            tcpPorts               = $TCPPorts
            udpPorts               = $UDPPorts
            expressRoute           = $Endpoint.expressRoute
            class               = $Endpoint.Class
            required               = $Endpoint.required
        }
        $Whole += $Merchandise
    }

    #Export knowledge to specified $CSVPath if specified
    if ($CSVPath) {
        strive {
            New-Merchandise -Path $CSVPath -ItemType File -Pressure:$true -ErrorAction Cease | Out-Null
            $Whole | Kind-Object serviceAreaDisplayName | Export-Csv -Path $CSVPath -Encoding UTF8 -Delimiter ';' -NoTypeInformation
            Write-Host ("Saved outcomes to {0} `nDone!" -f $CSVPath) -ForegroundColor Inexperienced
        }
        catch {
            Write-Warning ("Couldn't save outcomes to {0}" -f $CSVPath)
        }
    }
    else  Kind-Object serviceAreaDisplayName 
}

Obtain the script(s) from GitHub right here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments