Incase anyone wants to track a windows PC...

KC9MNEa year ago

I have a fleet of windows surface tablets that I maintain and I wanted to see if we can add them to traccar.
This is the powershell script I hashed out. I will probably make it into a windows service executable or something later. I figured if anyone needs something this would get you in the right direction.

#Pauls Easy GPS polling script

#dump imei for id and clean output
$imeitemp = netsh mbn show interface | findstr 355
$imei = $imeitemp.Substring($imeitemp.length-15)
#spawn a GeoCoordinateWatcher to get position data from windows
Add-Type -AssemblyName System.Device
$geowatcher = New-Object System.Device.location.GeoCoordinateWatcher
$geowatcher.start()
$geowatcher.status

Function SendLocation {
#get vars to send
$latitude = $geowatcher.position.location.latitude
$longitude = $geowatcher.position.location.longitude
$mph = $geowatcher.position.location.speed
$altitude = $geowatcher.position.location.altitude
$bat = (Get-WmiObject win32_battery).estimatedChargeRemaining
$accu = $geowatcher.position.location.horizontalaccuracy
#get timestamp and format it into universal time
$timestamp = [int][double]::Parse((get-date -date (Get-Date).ToUniversalTime() -UFormat %s))
#Create URL for traccar server
$traccar = "http://*YOURSERVERHERE*:5055/?id="+($imei)+"&lat="+($latitude)+"&lon="+($longitude)+"×tamp="+($timestamp)+"&altitude="+($altitude)+"&speed="+($mph)+"&battery="+($bat)+"&accuracy="+($accu)

#Send Location string to server aka post the url
Invoke-WebRequest -Uri $traccar
}

while ($true)
{
    #Call Send Location function every 30sec
SendLocation
#Set interval period here
start-sleep -seconds 30
}
KC9MNEa year ago

To clarify the IMEI section Surface imei's start with 355 so you would just need to change this to the first few numbers of your vender's imei.

you could also just set $imei to another system var or an arbitrary value too.

KC9MNEa year ago

I have this deployed now, I ended up using PS2EXE and NSSM to turn the powershell script into an executable that runs as a service so it pings traccar every 60sec when the computer is not sleeping. It appears to be working well.

RichardGa year ago

Nice one. How about using the serial number? $imei = wmic bios get serialnumber ??

I'm thinking that this could be done as a proactive remediation if you use intune. Similar to how Leanlaps is done: https://www.lieben.nu/liebensraum/2021/06/lightweight-laps-solution-for-intune-mde/

KC9MNEa year ago

you want to use the serial as the ID or include it as a data point?

if you want it as a data point reported to Traccar

you would just add a temp var like $serialraw = Get-CIMInstance win32_bios | format-list SerialNumber

this would return the serial number but with some formatting and header. like this

SerialNumber : 123456

but we just want the number so we use .Remove to strip the prepended text.

so we create a final $serial var and point it to $serial = $serialraw.Remove(0,16)

This will leave $serial as just the number part stripping the preceding 16 char.

now you have a var that is the serial and you just need to send it.

this is the line that sends data to traccar.

$traccar = "http://*YOURSERVERHERE*:5055/?id="+($imei)+"&lat="+($latitude)+"&lon="+($longitude)+"×tamp="+($timestamp)+"&altitude="+($altitude)+"&speed="+($mph)+"&battery="+($bat)+"&accuracy="+($accu)

you would just add the new var with a text ID
$traccar = "http://*YOURSERVERHERE*:5055/?id="+($imei)+"&lat="+($latitude)+"&lon="+($longitude)+"×tamp="+($timestamp)+"&altitude="+($altitude)+"&speed="+($mph)+"&battery="+($bat)+"&accuracy="+($accu)**+"&serial="+($serial)**

if you just want to use the serial as the ID can you could do the above variable part then just change the send line part from

http://*YOURSERVERHERE*:5055/?id="+($imei)+"&lat=........

to

http://*YOURSERVERHERE*:5055/?id="+($serial)+"&lat=........

this would send the serial as the id instead of the imei.

hope this helps.