Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregopenskycmd.ps

(view source code of airregopenskycmd.ps as plain text)

  1. <#
  2. .SYNOPSIS
  3. Search the downloaded OpenSky Network aircraft registration database for an aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
  4.  
  5. .DESCRIPTION
  6. First create a subdirectory 'OpenSkyNetwork' in this script's parent directory.
  7. Next download the OpenSky Network aircraft registration database (see links section), we will be using aircraft-database-complete-{year}-{month}.csv only for this script.
  8. Run this script with an aircraft registration as its only parameter (see examples section).
  9. The script will try to find the registration in the downloaded aircraft registry.
  10. If a match is found, the script will display a tab-delimited string with the registration and the manufacturer/model (<registration><tab><tab><manufacturer_and_model>).
  11. If the script was started by another PowerShell script with the -NoBreak switch, the calling PowerShell script may also read the manufacturer/model from the variable $Manufacturer, passed on by this script.
  12. If the script was started by a batch file, the calling batch file can use 'FOR /F' on this PowerShell script's screen output to find the manufacturer/model.
  13. Get-Help './AirRegOpenSkyCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  14.  
  15. .PARAMETER Registration
  16. A valid aircraft registration, e.g. N1944S, OK-NUL or OO-CAT
  17.  
  18. .PARAMETER Quiet
  19. Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1
  20.  
  21. .PARAMETER NoBreak
  22. Do not break the search loop after a match is found.
  23. On average searches take about twice as long when using this switch; hence it is recommended to use it only if:
  24.   -*-  this script is called by another PowerShell script
  25. and:
  26.   -*-  the calling PowerShell script uses the variables $Manufacturer and $Model set by this script
  27.  
  28. .PARAMETER Help
  29. Show the script's help screen
  30.  
  31. .PARAMETER Version
  32. Show this script's version number; if combined with -Verbose show full script path, version number and last/modified/release date
  33.  
  34. .PARAMETER Debug
  35. Show some progress messages; implies -NoBreak
  36.  
  37. .OUTPUTS
  38. If a match is found: a tab-delimited string with <registration><tab><manufacturer_and_model>, and if -NoBreak switch is used: variables $Manufacturer set to aircraft manufacturer $Model set to aircraft model
  39. If no match is found: False
  40.  
  41. .EXAMPLE
  42. . ./AirRegOpenSkyCmd.ps1 "OO-CAT"
  43. Will return tab-delimited string "OO-CAT<tab>Amateur Built<tab>Jodel D.112"
  44.  
  45. .EXAMPLE
  46. "OO-CAT" | . ./AirRegOpenSkyCmd.ps1 -NoBreak
  47. Will return tab-delimited string "OO-CAT<tab>Amateur Built<tab>Jodel D.112" and set variables $Manufacturer to "Amateur Built" and $Model to "Jodel D.112"
  48.  
  49. .EXAMPLE
  50. . ./AirRegOpenSkyCmd.ps1 -Version -Verbose
  51. Will return the full script path, version and last modified/release date
  52.  
  53. .EXAMPLE
  54. . ./AirRegOpenSkyCmd.ps1 -Version
  55. Will return the script version
  56.  
  57. .EXAMPLE
  58. . ./AirRegOpenSkyCmd.ps1 OO-CAT -Debug
  59. Will return:
  60.  
  61. Started searching for OO-CAT in ".\OpenSkyNetwork\aircraftDatabase.csv" at <date> <time>
  62. Found a match at <date> <time>
  63. OO-CAT  Amateur Built   Jodel D.112
  64. Finished at <date> <time> (time elapsed <elapsed time>)
  65.  
  66. .EXAMPLE
  67. Create and run the following PowerShell script:
  68. ===============================================================
  69. $Registration = 'OO-CAT' ; $Model = ''
  70. [void] ( . "$PSScriptRoot\AirRegOpenSkyCmd.ps1" -Registration $Registration -NoBreak )
  71. Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel        : {2}" -f $Registration, $Manufacturer, $Model )
  72. ===============================================================
  73.  
  74. Besides setting variables $Manufacturer to "Amateur Built" and $Model to "Jodel D.112", it will return:
  75.  
  76. Registration : OO-CAT
  77. Manufacturer : Amateur Built
  78. Model        : Jodel D.112
  79.  
  80. .EXAMPLE
  81. Create and run the following batch file:
  82. ===============================================================
  83. REM Note that there should only be a TAB and nothing else between delims= and the doublequote
  84. FOR /F "tokens=1-3 delims=	" %%A IN ('powershell . ./AirRegOpenSkyCmd.ps1 OO-CAT') DO (
  85. 	ECHO Registration : %%A
  86. 	ECHO Manufacturer : %%B
  87. 	ECHO Model        : %%C
  88. )
  89. ===============================================================
  90.  
  91. It will return:
  92.  
  93. Registration : OO-CAT
  94. Manufacturer : Amateur Built
  95. Model        : Jodel D.112
  96.  
  97. .LINK
  98. Script written by Rob van der Woude:
  99. https://www.robvanderwoude.com/
  100.  
  101. .LINK
  102. OpenSky Network aircraft registration database:
  103. https://opensky-network.org/datasets/metadata/
  104.  
  105. .LINK
  106. Capture -Debug and -Verbose parameter by mklement0 on StackOverflow.com:
  107. https://stackoverflow.com/a/48643616
  108. #>
  109.  
  110. param (
  111. 	[parameter( ValueFromPipeline )]
  112. 	[ValidatePattern("(^\s*$|[\?/]|^\w[\w-]{2,3}\w+)")]
  113. 	[string]$Registration,
  114. 	[switch]$Quiet,
  115. 	[switch]$NoBreak,
  116. 	[switch]$Help,
  117. 	[switch]$Version
  118. )
  119.  
  120. $progver = "1.02"
  121.  
  122. $Registration = $Registration.ToUpper( )
  123. $Manufacturer = ''
  124. $Model = ''
  125. [bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  126. [bool]$Verbose = ( $PSBoundParameters.ContainsKey( 'Verbose' ) )
  127.  
  128. if ( $Version ) {
  129. 	if ( $Verbose ) {
  130. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  131. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  132. 			"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  133. 		} else {
  134. 			# if last modified time is not equal to program version, the script has been tampered with
  135. 			"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  136. 		}
  137. 	} else {
  138. 		$progver
  139. 	}
  140. 	exit 0
  141. }
  142.  
  143. if ( $Help -or [string]::IsNullOrWhiteSpace( $Registration ) -or ( $Registration -match "[/\?]" ) ) {
  144. 	Clear-Host
  145. 	Write-Host ( "`"{0}`", Version {1}" -f $PSCommandPath, $progver ) -NoNewline
  146. 	$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  147. 	if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  148. 		Write-Host ", release date " -NoNewline
  149. 	} else {
  150. 		# if last modified time is not equal to program version, the script has been tampered with
  151. 		Write-Host ", last modified date " -NoNewline
  152. 	}
  153. 	Write-Host $lastmod.ToString( "yyyy-MM-dd" )
  154. 	Write-Host
  155. 	Get-Help $PSCommandPath -Full
  156. 	exit -1
  157. }
  158.  
  159. $found = $false
  160. $dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'OpenSkyNetwork' )
  161. if ( Test-Path -Path $dbfolder -PathType 'Container' ) {
  162. 	$dbfile = $( Get-ChildItem -Path $dbfolder -Name aircraft-database-complete-*.csv | Sort-Object | Select-Object -Last 1 )
  163. 	$dbfile = ( Join-Path -Path $dbfolder -ChildPath $dbfile )
  164. 	if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
  165. 		if ( $Debug ) {
  166. 			$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
  167. 			Write-Host ( "Started searching for {0} in `"{1}`" at {2}" -f $Registration, $dbfile, ( Get-Date ) )
  168. 		}
  169. 		( Get-Content $dbfile ).Split( "`n" ) | Select-String -Pattern "`"$Registration`"" -AllMatches | ForEach-Object {
  170. 			if ( !$found ) {
  171. 				$fields = $_.ToString( ).Split( ',' )
  172. 				if ( $fields[1] -match "`"$Registration`"" ) {
  173. 					if ( $Debug ) {
  174. 						Write-Host ( "Found a match at {0}" -f ( Get-Date ) )
  175. 					}
  176. 					$Manufacturer = $fields[3]
  177. 					$Model = $fields[4]
  178. 					( "{0}`t{1}`t{2}" -f $fields[1], $Manufacturer, $Model ) -replace '"',''
  179. 					if ( $Debug ) {
  180. 						Write-Host ( ( "{0}`t{1}`t{2}" -f $fields[1], $Manufacturer, $Model ) -replace '"','' )
  181. 					}
  182. 					$found = $true
  183. 					if ( -not $NoBreak ) {
  184. 						if ( $Debug ) {
  185. 							Write-Host ( "Finished at {0} (time elapsed {1})" -f ( Get-Date ), $StopWatch.Elapsed )
  186. 							$StopWatch.Stop( )
  187. 						} else {
  188. 							exit 0
  189. 						}
  190. 					}
  191. 				}
  192. 			}
  193. 		}
  194. 		if ( $Debug ) {
  195. 			Write-Host ( "Finished at {0} (time elapsed {1})" -f ( Get-Date ), $StopWatch.Elapsed )
  196. 			$StopWatch.Stop( )
  197. 		}
  198. 	} else {
  199. 		if ( -not $Quiet ) {
  200. 			Write-Host ( "Database file `"{0}`" not found" -f $dbfile )
  201. 		}
  202. 	}
  203. } else {
  204. 	if ( -not $Quiet ) {
  205. 		Write-Host ( "Folder `"{0}`" not found" -f $dbfolder )
  206. 	}
  207. }
  208. if ( !$found ) {
  209. 	$false
  210. }
  211.  

page last modified: 2024-04-16; loaded in 0.0095 seconds