Skip to main content

The Script V8

This is the current version of the Powershell script to track windows computers with GPS receivers.

Execute the script in a PowerShell window, this will keep a terminal open and display responses from the server.

Update the $devid variable with the assigned device ID from the server first!

Currently the script is set to poll the GPS and update the server every 30 seconds - change this as needed and enjoy.

Updates:

  • Version 8 - 16AUG2024 - New information collected and reported for the charging state of the device
$devid = "123abc" # Enter Your Device ID

Add-Type -AssemblyName System.Device

$geowatcher = New-Object System.Device.location.GeoCoordinateWatcher

$geowatcher.Start()

$geowatcher.Status
Function SendLocation {
    # Wait until valid geocoordinates are available
    while ($geowatcher.Position.Location.IsUnknown) {
        Start-Sleep -Seconds 10
    }    
    $latitude = $geowatcher.Position.Location.Latitude
    $longitude = $geowatcher.Position.Location.Longitude
    $speed = $geowatcher.Position.Location.Speed
    $bearing = $geowatcher.Position.Location.Course
    $altitude = $geowatcher.Position.Location.Altitude
    $accu = $geowatcher.Position.Location.HorizontalAccuracy

    # Check for NaN and replace with 0
    if ($latitude -ne $latitude) { $latitude = 0 }
    if ($longitude -ne $longitude) { $longitude = 0 }
    if ($speed -ne $speed) { $speed = 0 }
    if ($bearing -ne $bearing) { $bearing = 0 }
    if ($altitude -ne $altitude) { $altitude = 0 }
    if ($accu -ne $accu) { $accu = 0 }

    $batteryInfo = Get-WmiObject -Class Win32_Battery
    $batt = $batteryInfo.EstimatedChargeRemaining
    $batteryStatus = $batteryInfo.BatteryStatus
    $charging = $batteryStatus -eq 1
    $timestamp = [int][double]::Parse((Get-Date -date (Get-Date).ToUniversalTime() -UFormat %s))
    $traccar = "https://somethingsomethingdarkside.derp/" # Enter Your Traccar Server URL or IP

    $postData = @{
        'id'        = $devid
        'timestamp' = $timestamp
        'lat'       = $latitude
        'lon'       = $longitude
        'speed'     = $speed
        'bearing'   = $bearing
        'altitude'  = $altitude
        'accuracy'  = $accu
        'batt'      = $batt
        'charge'    = $charging
    }

    Invoke-WebRequest -Uri $traccar -Method Post -Body $postData

    Write-Host "Latitude: $latitude"
    Write-Host "Longitude: $longitude"
    Write-Host "Speed: $speed"
    Write-Host "Bearing: $bearing"
    Write-Host "Altitude: $altitude"
    Write-Host "Battery: $batt"
    Write-Host "Charging: $charging"
    Write-Host "Accuracy: $accu"
}

while ($true) {
    SendLocation
    Start-Sleep -Seconds 30
}