PublicIP.psm1

#region - From private/common.ps1
$script:providerMap = @{
    MyIP   = 'https://api.myip.com/'
    IPInfo = 'https://ipinfo.io/json'
}

$script:IPConfigFilePath = "$([Environment]::GetFolderPath('MyDocuments'))\IPConfig.json"
#endregion - From private/common.ps1

#region - From public/Get-GeoLocation.ps1
function Get-GeoLocation {
    Add-Type -AssemblyName System.Device #Required to access System.Device.Location namespace
    $GeoWatcher = New-Object System.Device.Location.GeoCoordinateWatcher #Create the required object
    $GeoWatcher.Start() #Begin resolving current locaton

    while (($GeoWatcher.Status -ne 'Ready') -and ($GeoWatcher.Permission -ne 'Denied')) {
        Start-Sleep -Milliseconds 100 #Wait for discovery.
    }
    if ($GeoWatcher.Permission -eq 'Denied') {
        Write-Error 'Access Denied for Location Information'
    } else {
        $Location = $GeoWatcher.Position.Location | Select-Object Latitude, Longitude #Select the relevent results.
    }
    $GeoWatcher.Stop()
    return $Location
}
#endregion - From public/Get-GeoLocation.ps1

#region - From public/Get-IPConfig.ps1
function Get-IPConfig {
    $PublicIP = Get-PublicIP
    $Time = Get-Date -Format yyyyMMdd-hhmmss.fffff
    $Location = Get-GeoLocation
    $LocalIPConfig = Get-NetIPAddress -AddressFamily IPv4 -Type Unicast | Where-Object PrefixOrigin -Match dhcp
    $IPObj = [ordered]@{
        PublicIP  = $PublicIP.IP
        PrivateIP = $LocalIPConfig.IPv4Address
        PCName    = $ENV:COMPUTERNAME
        Time      = $Time
        Latitude  = $Location.Latitude
        Longitude = $Location.Longitude
    }
    return $IPObj
}
#endregion - From public/Get-IPConfig.ps1

#region - From public/Get-PublicIP.ps1
function Get-PublicIP {
    param(
        [Parameter(Mandatory = $false)]
        [ValidateScript({ $providerMap.Keys })]
        $Provider = 'IPInfo'
    )

    Invoke-RestMethod -Uri $providerMap[$Provider]
}
#endregion - From public/Get-PublicIP.ps1

#region - From public/Restore-IPConfig.ps1
function Restore-IPConfig {
    if (Test-Path -Path $IPConfigFilePath) {
        return Get-Content -Path $IPConfigFilePath | ConvertFrom-Json
    }
}
#endregion - From public/Restore-IPConfig.ps1

#region - From public/Save-IPConfig.ps1
function Save-IPConfig {
    $IPConfig = @()
    $RestoredIPConfig = Restore-IPConfig
    if ($null -ne $RestoredIPConfig) {
        $IPConfig += $RestoredIPConfig
    }
    $CurrentIPConfig = Get-IPConfig

    # Check if recent and current ip is the same
    if ($IPConfig[-1].PublicIP -eq $CurrentIPConfig.PublicIP) {

    } else {
        "Public IP has changed since $($IPConfig[-1].Time)"
        "Old PIP: $($IPConfig[-1].PublicIP)"
        "New PIP: $($CurrentIPConfig.PublicIP)"
        $IPConfig += $CurrentIPConfig

        if (-not (Test-Path -Path $IPConfigFilePath)) {
            New-Item -Path $IPConfigFilePath -Force | Out-Null
        }

        $IPConfig | ConvertTo-Json -AsArray | Set-Content -Path $IPConfigFilePath -Force
    }
}
#endregion - From public/Save-IPConfig.ps1

Export-ModuleMember -Function 'Get-GeoLocation','Get-IPConfig','Get-PublicIP','Restore-IPConfig','Save-IPConfig' -Cmdlet '*' -Variable '*' -Alias '*'