Your first tracking client
Brett Goodlad
??7 x LinkedIn Top Voice | Experienced Full-Stack Expert | Delivering digital solutions from Front to Back End | Leveraging Performance, Efficiency & Improvement
If you've not already had a look at the first article in the series, then be sure to check that out first before you move onto this one.
This time we'll be making your first client application, in the form of an installable Windows service.
I'll demonstrate how to do the following:
Requirements
That's about it. Everything else you should already have at your disposal.
Tracking Powershell Script
Lets start with the key player here first. The script that enables location services on your Windows device, then sends the location information to your server periodically.
Create a new directory wherever you want to work from and make a new file named wTrack.ps1 and paste in the following code, replacing yourserver with your actual server address we established in the previous article.
Function Get-UserLocation {
$Name = "LetAppsAccessLocation"
$Value = 1
$Path = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\AppPrivacy"
If ((Test-Path $Path) -eq $false){
New-Item -Path $Path -ItemType Directory
}
If (-!(Get-ItemProperty -Path $Path -Name $name -ErrorAction SilentlyContinue)){
New-ItemProperty -Path $Path -Name $Name -Value $Value
} else {
Set-ItemProperty -Path $Path -Name $Name -Value $Value
}
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 {
$latitude = $Geowatcher.Position.Location.Latitude
$longitude = $Geowatcher.Position.Location.Longitude
}
$global:progressPreference = 'silentlyContinue'
$serverAddress = "https://yourserver/data/?lat="+($latitude)+"&lon="+($longitude)
Invoke-WebRequest -Uri $serverAddress
} while ($true) {
Get-UserLocation
start-sleep -seconds 300
}
In the first part we're accessing the system registry and turning on location services. Next we're grabbing the location data. Finally we're deciding how frequently to send this information. Feel free to change the frequency if necessary.
NSSM Extraction
Let's assume you downloaded NSSM earlier. If not, do that now.
Open the zip file and navigate to the win64 folder where you should see two files like below.
Copy those files into your working directory where you created your wTrack.ps1 file previously. We'll need these later.
NSSM Installation Script
NSSM stands for Non-Sucking Service Manager ??
Essentially it allows you to create and configure Windows services. We'll be needing this to make our tracking client run in the background and boot with Windows as a service.
NSSM is a command line tool, so we can create another Powershell script that will execute the necessary commands we desire to setup our background service.
Create another file in your working directory named install.ps1 and paste in the following code.
New-Item -ItemType Directory -Path "C:\wTrack"
Copy-Item "wTrack.exe" -Destination "C:\wTrack"
.\nssm install wTrack C:\wTrack\wTrack.exe
.\nssm set wTrack AppDirectory C:\wTrack
.\nssm set wTrack DisplayName wTrack
.\nssm set wTrack Start SERVICE_AUTO_START
.\nssm set wTrack AppNoConsole 1
.\nssm set wTrack AppStopMethodConsole 30000
.\nssm start wTrack
Here we're creating a new directory, copying our executable script we want to run as the service, then setting our parameters, which I think are fairly self-explanatory (I hope). You may want to hide your client better than I have above, or give it a more inconspicuous name in the service list, that's up to you. Feel free to make the changes above as desired.
Compiling Powershell Scripts
In the script above, we referred to our previously made Powershell files as .exe rather than .ps1. This is where we turn them into executable files using PS2EXE.
Open Powershell as Administrator and type the following command
Install-Module -Name ps2exe
Navigate to the installation folder and launch Win-PS2EXE
We're now going to convert both of our ps1 files to exe files.
Change the source and target to suit (your working directory) and populate the options as below. Repeat this for both Powershell files. There's no real need to set an icon or any other information, but it is a cool little app which you may want to do that for other purposes in the future.
领英推荐
Your working directory should now look a little something like this
The ps1 files at this point are no longer needed, so feel free to delete them, however it really doesn't make any difference.
Creating Package Installer
Now we've created all the necessary files we can package them up into a nice and tidy installer that we simply double click and it does the rest.
Open a command prompt and type iexpress
Click next, then click next again on the next screen where extract files and run is selected.
Package title can be anything you like, but i'll stick with wTrack.
Select don't display prompt on the next screen and don't display license on the one after that.
Once we have to pick our Packaged files, we're going to add our two .exe files and NSSM like so:
Our install program is the one we created earlier. This will create the folder, copy the client and set it up as a service.
We can leave the next Show Window option as default or feel free to change that if necessary.
The Finished Message can be anything you choose. I'll write "Congratulations!". Depending on your intent, you may wish to suppress all forms of notifications, or if you're an honest individual like me that won't matter ??
The final screen asking for the package name can be anything you want. I'm browsing to my working folder and naming it wTrack Installer.
You can select No Restart on the final screen as it isn't needed to launch the service for the first time.
Awesome ?? Now everything is packaged up into a nice and tidy install file we simply need to run on our target machine. Let's give it a try ??
Right click the file and Run as admin.
Now if we open Task Manager and take a look at the running services, we should see our tracking client in action ??
If you've entered your server details properly then your map we created in the previous article should've updated with your computers current location.
Cool, I'm in Paris! We'll cover location spoofing for testing purposes in a later article ??
This service will now boot with Windows and send location data back to your GPS tracking server.
Final Thought
This will actually run and send data to any OsmAnd enabled tracking server. As far as I know there isn't another guide like this to create a Windows service in existence, so count yourself lucky you saw it here first! ??
Next time we'll venture onto an Android tracking client. Go ahead and download Android Studio ahead of time ??
Founder @Bles Software | Driving Success as Top Seller AI Solutions | 152+ Projects Delivered | 120+ Five-Star Ratings on Fiverr
5 个月The idea of converting Powershell to executables for GPS tracking is intriguing! This could revolutionize how we approach software packaging and deployment.
CEO/Principal: CERAC Inc. FL USA..... ?? ????????Consortium for Empowered Research, Analysis & Communication
6 个月Gotta love it......Keep rolling them out!!! ??
Exp. with NBA, Nike, AEG, Interscope, Google and ESPN | AAF OC Board Member
6 个月Awesome stuff, Brett! Love the detailed breakdown on PowerShell and package installers. Definitely worth a read!
TOP LINKEDIN VOICE EARNED BADGES / CONTENTS WRITERS/ HEAD OF OPERATION/HR AND OTHERS #linkedin.com/in/muhammad-suhail-669094271
6 个月Love this
Senior .Net Engineer | .Net Developer | Full Stack Developer | C# | .Net | Azure | React | Angular
6 个月Thanks for sharing