Skip to content

Get GPS Location of Windows Host

Video Lecture

Get GPS Location of Windows Host

Description

Create a new file using the script below and save it into the C:\temp\ folder.

getGPSLocation.ps1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Script from https://stackoverflow.com/a/46287884

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 {
    $GeoWatcher.Position.Location | Select Latitude,Longitude #Select the relevent results.
}

Execute the Script from a Command Prompt

To execute the script from a command line, open a PowerShell prompt and type,

powershell -NoProfile -ExecutionPolicy bypass -File C:\temp\getGPSLocation.ps1

If you get the error Access Denied for Location Information then,

  1. StartSettingsPrivacy & SecurityLocation

  2. Enable Location Services

  3. Enable Let apps access your location

  4. Enable Let desktop app access your location

  5. Try running the script again.

Execute the Script as a Zabbix Item

To execute the script as a Zabbix item, add a new UserParameter to the Zabbix agents configuration file,

UserParameter=getGPSLocation,powershell.exe -NoProfile -ExecutionPolicy bypass -File "C:\temp\getGPSLocation.ps1"

Test the new UserParameter from the command line first,

CD into the folder where Zabbix agent was installed.

cd C:\Program Files\Zabbix Agent
# or if using Zabbix agent 2
cd C:\Program Files\Zabbix Agent 2

Next, execute,

zabbix_agentd.exe -t getGPSLocation
# or if using Zabbix agent 2
zabbix_agent2.exe -t getGPSLocation

Create the Item on your Windows Host in Zabbix

Restart your Zabbix agent on your Windows host so the user parameter is registered.

Now create a new item on your windows host using the Zabbix UI.

Key Value
Name Get GPS Location
Type Zabbix agent (active or default passive)
Key getGPSLocation
Type of information Text
Update interval 10m

If after a while, you get the error Timeout occured while gathering data, then ensure that you have restarted your Zabbix agent.

If the error still occurs, try enabling the permission to allow the shell to access location data.

  1. StartSettingsPrivacy & SecurityLocation

  2. Enable Location Services

  3. Enable Let apps access your location

  4. Enable Windows Shell Experience Host

And also experiment with the Zabbix agent timeout setting,

AdministrationTimeoutsZabbix agent

Default is 3s. Try 10s up to 30s

Also, don't query the location too often. In my item settings above, I query once every 10 minutes.

Querying once every minute is likely to cause timeouts.

In fact, I often get timeouts regardless, I am unable to eradicate them completely.

View the GPS Location on the Map

See the video for further instructions on how to populate the latitude and longitude inventory fields from the response so that we can see the location on the map.

The regex used in the video was,

(-*[0-9]*\.[0-9]*) (-*[0-9]*\.[0-9]*)

Comments