-
Notifications
You must be signed in to change notification settings - Fork 1
/
Geo-Locate.ps1
89 lines (76 loc) · 2.9 KB
/
Geo-Locate.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#
.Synopsis
Retrieves the Computer's geographical location
.DESCRIPTION
Retrieves the Computer Geolocation using the Windows location platform and Google geocoding API
.EXAMPLE
Get-ComputerGeolocation
.NOTES
Version 1.0
Written by Alex Verboon
#>
function Get-ComputerGeoLocation ()
{
# Windows Location API
$mylocation = new-object –ComObject LocationDisp.LatLongReportFactory
# Get Status
$mylocationstatus = $mylocation.status
If ($mylocationstatus -eq "4")
{
# Windows Location Status returns 4, so we're "Running"
# Get Latitude and Longitude from LatlongReport property
$latitude = $mylocation.LatLongReport.Latitude
$longitude = $mylocation.LatLongReport.Longitude
if ($latitude -ne $null -or $longitude -ne $Null)
{
# Retrieve Geolocation from Google Geocoding API
$webClient = New-Object System.Net.WebClient
Write-host "Retrieving geolocation for" $($latitude) $($longitude)
$url = "https://maps.googleapis.com/maps/api/geocode/xml?latlng=$latitude,$longitude&sensor=true"
$locationinfo = $webClient.DownloadString($url)
$doc = $locationinfo
# Verify the response
if ($doc.GeocodeResponse.status -eq "OK")
{
$street_address = $doc.GeocodeResponse.result | Select-Object -Property formatted_address, Type | Where-Object -Property Type -eq "street_address"
$geoobject = New-Object -TypeName PSObject
$geoobject | Add-Member -MemberType NoteProperty -Name Address -Value $street_address.formatted_address
$geoobject | Add-Member -MemberType NoteProperty -Name latitude -Value $mylocation.LatLongReport.Latitude
$geoobject | Add-Member -MemberType NoteProperty -Name longitude -Value $mylocation.LatLongReport.longitude
$geoobject | format-list
}
Else
{
Write-Warning "Request failed, unable to retrieve Geo locatiion information from Geocoding API"
}
}
Else
{
write-warning "Latitude or Longitude data missing"
}
}
Else
{
switch($mylocationstatus)
{
# All possible status property values as defined here:
# http://msdn.microsoft.com/en-us/library/windows/desktop/dd317716(v=vs.85).aspx
0 {$mylocationstatuserr = "Report not supported"}
1 {$mylocationstatuserr = "Error"}
2 {$mylocationstatuserr = "Access denied"}
3 {$mylocationstatuserr = "Initializing" }
4 {$mylocationstatuserr = "Running"}
}
If ($mylocationstatus -eq "3")
{
write-host "Windows Loction platform is $mylocationstatuserr"
sleep 5
Get-ComputerGeoLocation
}
Else
{
write-warning "Windows Loction platform: Status:$mylocationstatuserr"
}
}
} # end function
Get-ComputerGeoLocation