Public/Network/Get-InternetConnection.ps1

FUNCTION Get-InternetConnection {
    <#
    .SYNOPSIS
    Retrieves and displays internet connection information, including optional speed test results.
 
    .DESCRIPTION
    This function collects various details about the internet connection, such as public IP address, geographic location, and ISP information. It can also perform a speed test to measure download and upload speeds, latency, and packet loss.
 
    .PARAMETER SpeedTest
    If specified, performs a speed test using the Ookla SpeedTest CLI tool.
 
    .PARAMETER IPAddress
    Specifies an IP address to retrieve geographic information for. If not provided, the current public IP address is used.
 
    .EXAMPLE
    Get-InternetInfo -SpeedTest
 
    This command retrieves internet connection information and performs a speed test.
 
    .EXAMPLE
    Get-InternetInfo -IPAddress "8.8.8.8"
 
    This command retrieves geographic information for the specified IP address.
 
    .NOTES
    This function requires internet access and may need to be run with appropriate permissions. The SpeedTest CLI tool must be installed in the specified directory for speed testing.
    #>


    [CmdletBinding()]
    PARAM ( )

    ### Declare Variables
    $Results = @()
    $GeoDataResults = $null
 

    ### IP and Geo Location Data
    $IPInfo = Get-IPInfo
   
    ### SpeedTest
    $InternetSpeed = Test-InternetSpeed
        
    ### Connection Information
    $NetConnection = Test-NetConnection

    $Adapter = Get-NetAdapter | Where-Object { $_.InterfaceAlias -eq $NetConnection.InterfaceAlias }
    $AdapterProtocol = $Null
    $SignalStrength = $Null
    $LinkSpeed = $Null
    IF ($Adapter.PhysicalMediaType -like "*802.3*") { 
        $AdapterProtocol = "802.3" 
        $SignalStrength = ""
        $ConnectedSSID = ""
    }
    ELSEIF ($Adapter.PhysicalMediaType -like "*802.11*") {
        $AdapterProtocol = netsh wlan show interfaces | select-string -Pattern "[\s]Radio Type"
        $AdapterProtocol = $AdapterProtocol.tostring().replace(" Radio type : ","")

        $SignalStrength = netsh wlan show interfaces | select-string -Pattern "[\s]Signal"
        $SignalStrength = $SignalStrength.tostring().replace(" Signal : ","")

        $ConnectedSSID = netsh wlan show interfaces | select-string -Pattern "[\s]SSID"
        $ConnectedSSID = $ConnectedSSID.tostring().replace(" SSID : ","")
    }
        
    $LinkSpeed = ([int]($Adapter.ReceiveLinkSpeed / 1000000)).ToString()+"\"+([int]($Adapter.TransmitLinkSpeed / 1000000)).ToString()

    $DNSTest = Test-DNSResolution

    [string]$DNSTestFailPercent = "$([math]::Round((($DNSTest.FailCount | Measure-Object -Sum).Sum / ($DNSTest.TestCount | Measure-Object -Sum).Sum) * 100,0))%"

    $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
        Time = $InternetSpeed.Time
        Hostname = $env:COMPUTERNAME
        PrivateIP = $NetConnection.SourceAddress
        AdapterName = $Adapter.ifAlias
        AdapterDescription = $Adapter.ifDesc
        AdapterProtocol = $AdapterProtocol
        LinkSpeed = $LinkSpeed
        SignalStrength = $SignalStrength
        SSID = $ConnectedSSID        
        PublicIP = $IPInfo.IP
        Country = $IPInfo.Country
        State = $IPInfo.State
        City = $IPInfo.City
        ZipCode = $IPInfo.ZipCode
        Org = $IPInfo.Org
        ISP = $IPInfo.ISP      
        DownloadSpeed = $InternetSpeed.DownloadSpeed
        DownloadLatencyAvg = $InternetSpeed.DownloadLatencyAvg
        DownloadLatencyHigh = $InternetSpeed.DownloadLatencyHigh
        UploadSpeed = $InternetSpeed.UploadSpeed
        UploadLatencyAvg = $InternetSpeed.UploadLatencyAvg
        UploadLatencyHigh = $InternetSpeed.UploadLatencyHigh
        PacketLoss = $InternetSpeed.PacketLoss
        DNSResolutionFailRate = $DNSTestFailPercent
    }
    
    RETURN $Results | Select Time,
                             Hostname,
                             PublicIP,
                             PrivateIP,
                             AdapterName,
                             AdapterDescription,
                             AdapterProtocol,
                             LinkSpeed,
                             SSID,
                             SignalStrength,
                             Country,
                             City,
                             State,
                             ZipCode,                                
                             ISP,
                             Org,
                             DownloadSpeed,
                             DownloadLatencyAvg,
                             DownloadLatencyHigh,
                             UploadSpeed,
                             UploadLatencyAvg,
                             UploadLatencyHigh,
                             PacketLoss,
                             DNSResolutionFailRate
}
New-Alias -Name Get-InternetInfo -Value Get-InternetConnection