Private/Get-UserLocation.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
35
36
37
38
Function Get-UserLocation {
    <#
        .SYNOPSIS
            Returns the location information for the local user.
 
        .EXAMPLE
            Get-UserLocation
 
            Result
            -----------
            Latitude : 123.281781267745
            Longitude : 456.05927473888778
            Altitude : 0
            HorizontalAccuracy : 82
            VerticalAccuracy : NaN
            Speed : NaN
            Course : NaN
            IsUnknown : False
    #>
      
    [cmdletbinding()]
    Param()

    Add-Type -AssemblyName System.Device 

    $GeoWatcher = New-Object System.Device.Location.GeoCoordinateWatcher
    $GeoWatcher.Start()

    while (($GeoWatcher.Status -ne 'Ready') -and ($GeoWatcher.Permission -ne 'Denied')) {
        Start-Sleep -Milliseconds 100 
    }  

    if ($GeoWatcher.Permission -eq 'Denied') {
        Throw 'Access was denied to user location information'
    }
    else {
        $GeoWatcher.Position.Location
    }
}