Category filter
Script to fetch location coordinates on Windows devices
Fetching the location coordinates helps identify where a device is currently located. The latitude and longitude coordinates of a device are crucial in determining the precise locations of remote workforce. This helps the IT admins ensure that employees use company-owned devices from permitted locations. Using Hexnode’s Execute Custom Script action you can execute scripts to obtain the exact location coordinates of a Windows device.
PowerShell script
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Add-Type -AssemblyName System.Device #Required to access System.Device.Location namespace $latitude_and_longitude = New-Object System.Device.Location.GeoCoordinateWatcher #Create the required object $latitude_and_longitude.Start() #Begin resolving current locaton while (($latitude_and_longitude.Status -ne 'Ready') -and ($latitude_and_longitude.Permission -ne 'Denied')) { Start-Sleep -Milliseconds 100 #Wait for discovery. } if ($latitude_and_longitude.Permission -eq 'Denied'){ Write-Error 'Access Denied for Location Information' } else { $latitude_and_longitude.Position.Location | Select Latitude,Longitude #Select the relevent results. } |
This script uses system.device.location(GeoCoordinateWatcher)
method to get the location coordinates. Once the action succeeds, you can view the location coordinates by navigating to the Action History page of the device on which the script has been executed. Click on Show Output corresponding to the Execute Custom Script action that had been initiated to view the location coordinates.