Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregncmd.ps

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

  1. <#
  2. .SYNOPSIS
  3. Search a downloaded FAA 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 'N' in this script's parent folder.
  7. Next, download the FAA Aircraft Registry's Releasable Aircraft Database (see links section), unzip it and move the files MASTER.txt and ACFTREF.txt (and optionally the other files as well) to the 'N' folder.
  8. Now run this script with an aircraft registration as its only parameter (see examples section).
  9. The script will first look up the MfgrModelCode for the specified registration code in the MASTER.txt file.
  10. With the MfgrModelCode the script will look up the manufacturer and aircraf model in the ACFTREG.txt file.
  11. If a match is found, the script will display a tab-delimited string with the registration, the manufacturer and the aircraft model (<registration><tab><manufacturer><tab><model>).
  12. If the script was started by another PowerShell script, the calling PowerShell script may also read the manufacturer and model from the variables $Manufacturer and $Model, passed on by this script.
  13. 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 and model.
  14. Get-Help './AirRegNCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  15. The script contains a "second" script in a comment block, showing the "official' way to deal with the database's CSV files; however, the method used, regular expressions on plain text files, is about 4 times faster.
  16.  
  17. .PARAMETER Registration
  18. A valid FAA aircraft registration, i.e. Nxxxxx (where x is a single alphanumeric character/digit)
  19.  
  20. .PARAMETER Quiet
  21. Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1.
  22.  
  23. .PARAMETER Help
  24. Show the script's help screen
  25.  
  26. .PARAMETER Debug
  27. Show some progress messages
  28.  
  29. .OUTPUTS
  30. A tab-delimited string <registration><tab><manufacturer><tab><model> and manufacturer and model are also stored in output variables $Manufacturer and $Model.
  31.  
  32. .EXAMPLE
  33. . ./AirRegNCmd.ps1 "N1944S"
  34. Will return tab-delimited string "N1944S<tab>BOEING<tab>E75", and set variables $Manufacturer to "BOEING" and $Model to "E75"
  35.  
  36. .EXAMPLE
  37. "N1944S" | . ./AirRegNCmd.ps1
  38. Will also return tab-delimited string "N1944S<tab>BOEING<tab>E75", and set variables $Manufacturer to "BOEING" and $Model to "E75"
  39.  
  40. .EXAMPLE
  41. . ./AirRegNCmd.ps1 "N9ZX" -Debug
  42. This will return:
  43.  
  44. Start searching 9ZX in MASTER file at <date> <time>
  45. Start searching 05655US in ACFTREF file at <date> <time>
  46. N9ZX<tab>POBEREZNY PAUL H<tab>HIPERLIGHT SNS-8
  47.  
  48. Finished at <date> <time> (elapsed time <time elapsed>)
  49.  
  50. .EXAMPLE
  51. Create and run the following PowerShell script:
  52. ===============================================================
  53. $Registration = 'N1944S' ; $Manufacturer = '' ; $Model = ''
  54. [void] ( . "$PSScriptRoot\AirRegNCmd.ps1" -Registration $Registration )
  55. Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel        : {2}" -f $Registration, $Manufacturer, $Model )
  56. ===============================================================
  57.  
  58. Besides setting variables $Manufacturer to "BOEING" and $Model to "E75", it will return:
  59.  
  60. Registration : N1944S
  61. Manufacturer : BOEING
  62. Model        : E75
  63.  
  64. .EXAMPLE
  65. Create and run the following batch file:
  66. ===============================================================
  67. REM Note that there should only be a TAB and nothing else between delims= and the doublequote
  68. FOR /F "tokens=1-3 delims=	" %%A IN ('powershell . ./AirRegNCmd.ps1 N1944S') DO (
  69. 	ECHO Registration : %%A
  70. 	ECHO Manufacturer : %%B
  71. 	ECHO Model        : %%C
  72. )
  73. ===============================================================
  74.  
  75. It will return:
  76.  
  77. Registration : N1944S
  78. Manufacturer : BOEING
  79. Model        : E75
  80.  
  81. .LINK
  82. Script written by Rob van der Woude:
  83. https://www.robvanderwoude.com/
  84.  
  85. .LINK
  86. FAA Aircraft Registry's Releasable Aircraft Database:
  87. https://www.faa.gov/licenses_certificates/aircraft_certification/aircraft_registry/releasable_aircraft_download/
  88.  
  89. .LINK
  90. Capture -Debug parameter by mklement0 on StackOverflow.com:
  91. https://stackoverflow.com/a/48643616
  92. #>
  93.  
  94. param (
  95. 	[parameter( ValueFromPipeline )]
  96. 	[ValidatePattern("(^\s*$|[\?/-]|^N[CLPRSX]?[0-9][0-9A-Z]{2,4}$)")]
  97. 	[string]$Registration,
  98. 	[switch]$Quiet,
  99. 	[switch]$Help
  100. )
  101.  
  102. $progver = "1.00"
  103.  
  104. $Registration = $Registration.ToUpper( )
  105. [string]$Manufacturer = ''
  106. [string]$Model = ''
  107. [bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  108.  
  109. function ShowHelp( $errormessage = '' ) {
  110. 	if ( !$Quiet ) {
  111. 		if ( $errormessage ) {
  112. 			Write-Host
  113. 			Write-Host "Error: " -ForegroundColor Red -NoNewline
  114. 			Write-Host $errormessage
  115. 		}
  116. 		Write-Host
  117. 		Write-Host ( "AirRegNCmd.ps1,  Version {0}" -f $progver )
  118. 		Write-Host "Search downloaded FAA aircraft registration database for a registation"
  119. 		Write-Host
  120. 		Write-Host "Usage:  " -NoNewline
  121. 		Write-Host ". ./AirRegNCmd.ps1 [-Registration] N**** [-Quiet] [-Debug] [-Help]" -ForegroundColor White
  122. 		Write-Host
  123. 		Write-Host "Where:   " -NoNewline
  124. 		Write-Host "N****           " -NoNewline -ForegroundColor White
  125. 		Write-Host "is a valid FAA aircraft registration, e.g. N1944S"
  126. 		Write-Host "         -Quiet          " -NoNewline -ForegroundColor White
  127. 		Write-Host "all errors are ignored and no error messages displayed"
  128. 		Write-Host "         -Debug          " -NoNewline -ForegroundColor White
  129. 		Write-Host "shows some progress messages"
  130. 		Write-Host "         -Help           " -NoNewline -ForegroundColor White
  131. 		Write-Host "shows this help screen"
  132. 		Write-Host
  133. 		Write-Host "Notes:   This script requires a downloaded FAA aircraft registration database,"
  134. 		Write-Host "         located in a subfolder 'N' of this script's parent folder."
  135. 		Write-Host "         The FAA aircraft registration database can be downloaded at:"
  136. 		Write-Host "         https://www.faa.gov/licenses_certificates/aircraft_certification" -ForegroundColor DarkGray
  137. 		Write-Host "         /aircraft_registry/releasable_aircraft_download/" -ForegroundColor DarkGray
  138. 		Write-Host "         The result, if any, of the search is displayed as tab-delimited text:"
  139. 		Write-Host "         <registration><tab><manufacturer><tab><model>"
  140. 		Write-Host "         Besides its screen output, this script will also set the `$Manufacturer"
  141. 		Write-Host "         and `$Model variables with the database search result."
  142. 		Write-Host "         Run " -NoNewline
  143. 		Write-Host "Get-Help `"./AirRegNCmd.ps1`" -Examples " -NoNewline -ForegroundColor White
  144. 		Write-Host "for some examples of"
  145. 		Write-Host "         `"nesting`" this script in other PowerShell or batch scripts."
  146. 		Write-Host "         Return code (`"ErrorLevel`") 1 in case of errors, otherwise 0."
  147. 		Write-Host
  148. 		Write-Host "Credits: Code to capture -Debug parameter by mklement0 on StackOverflow.com:"
  149. 		Write-Host "         https://stackoverflow.com/a/48643616" -ForegroundColor DarkGray
  150.  
  151. 		Write-Host
  152. 		Write-Host "Written by Rob van der Woude"
  153. 		Write-Host "https://www.robvanderwoude.com"
  154. 	}
  155. 	Exit 1
  156. }
  157.  
  158. if ( $Help -or $Registration -match "(^\s*$|[\?/-])" ) {
  159. 	ShowHelp
  160. 	Exit 1
  161. }
  162.  
  163. $N_number = $Registration -replace "^N[CLPRSX]?",""
  164.  
  165. $dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'N' )
  166. $masterfile = ( Join-Path -Path $dbfolder -ChildPath 'MASTER.txt' )
  167. $acftreffile = ( Join-Path -Path $dbfolder -ChildPath 'ACFTREF.txt' )
  168.  
  169. if ( ( Test-Path -Path $masterfile -PathType 'Leaf' ) -and ( Test-Path -Path $acftreffile -PathType 'Leaf' ) ) {
  170. 	<#
  171. 	# Method 1: treat files as CSV
  172. 	# Time elapsed during test runs: approximately 140 seconds
  173. 	#
  174. 	if ( $Debug ) {
  175. 		$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
  176. 		Write-Host ( "Start reading MASTER table at {0}" -f ( Get-Date ) )
  177. 	}
  178. 	$found = $false # will be set to True when a matching aircraft is found in the database
  179. 	$mastertable = Import-Csv -Path $masterfile -Delimiter ','
  180. 	if ( $Debug ) {
  181. 		Write-Host ( "Start reading ACFTREF table at {0}" -f ( Get-Date ) )
  182. 	}
  183. 	$acftreftable = Import-Csv -Path $acftreffile -Delimiter ','
  184. 	if ( $Debug ) {
  185. 		Write-Host ( "Start searching {0} in MASTER table at {1}" -f $N_number, ( Get-Date ) )
  186. 	}
  187. 	foreach ( $record in $mastertable ) {
  188. 		if ( !$found ) {
  189. 			if ( $record.'N-NUMBER'.Trim( ) -eq $N_number ) {
  190. 				$ManufacturerModelCode = $record.'MFR MDL CODE'.Trim( )
  191. 				if ( $Debug ) {
  192. 					Write-Host ( "Start searching {0} in ACFTREF table at {1}" -f $ManufacturerModelCode, ( Get-Date ) )
  193. 				}
  194. 				foreach ( $item in $acftreftable ) {
  195. 					if ( !$found ) {
  196. 						if ( $item.'CODE' -eq $ManufacturerModelCode ) {
  197. 							$Manufacturer = $item.'MFR'.Trim( )
  198. 							$Model = $item.'MODEL'.Trim( )
  199. 							"{0}`t{1}`t{2}" -f $Registration.ToUpper( ), $Manufacturer, $Model | Out-String
  200. 							$found = $true
  201. 						}
  202. 					}
  203. 				}
  204. 			}
  205. 		}
  206. 	}
  207. 	if ( $Debug ) {
  208. 		Write-Host ( "Finished at {0} (elapsed time {1})`n`n" -f ( Get-Date ), $StopWatch.Elapsed )
  209. 		$StopWatch.Stop( )
  210. 	}
  211. 	#>
  212.  
  213. 	# Method 2: treat files as plain text and use regular expressions to find matches
  214. 	# Time elapsed during test runs: approximately 35 seconds, about 4 times as fast as method 1
  215. 	#
  216. 	if ( $Debug ) {
  217. 		$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
  218. 		Write-Host ( "Start searching {0} in MASTER file at {1}" -f $N_number, ( Get-Date ) )
  219. 	}
  220. 	$pattern = "^{0}\s*,[^\n\r]+" -f $N_number
  221. 	$record = ( ( Get-Content -Path $masterfile ) -match $pattern )
  222. 	if ( $record ) {
  223. 		$ManufacturerModelCode = $record.Split( ',' )[2].Trim( )
  224. 		if ( $Debug ) {
  225. 			Write-Host ( "Start searching {0} in ACFTREF file at {1}" -f $ManufacturerModelCode, ( Get-Date ) )
  226. 		}
  227. 		$pattern = "^{0}\s*,[^\n\r]+" -f $ManufacturerModelCode
  228. 		$record = ( ( Get-Content -Path $acftreffile ) -match $pattern )
  229. 		if ( $record ) {
  230. 			$Manufacturer = $record.Split( ',' )[1].Trim( )
  231. 			$Model = $record.Split( ',' )[2].Trim( )
  232. 		}
  233. 	}
  234. 	"{0}`t{1}`t{2}" -f $Registration.ToUpper( ), $Manufacturer, $Model | Out-String
  235. 	if ( $Debug ) {
  236. 		Write-Host ( "Finished at {0} (elapsed time {1})`n`n" -f ( Get-Date ), $StopWatch.Elapsed )
  237. 		$StopWatch.Stop( )
  238. 	}
  239. } else {
  240. 	if ( $Quiet ) {
  241. 		if ( $Debug ) {
  242. 			Write-Host "Downloaded FAA Aircraft Registry's Releasable Aircraft Database not found"
  243. 		}
  244. 		exit 1
  245. 	} else {
  246. 		$message = "No downloaded FAA Aircraft Registry's Releasable Aircraft Database was found.`n`nDo you want to open the download webpage for the database now?"
  247. 		$title   = "No Database Found"
  248. 		$buttons = "YesNo"
  249. 		Add-Type -AssemblyName 'System.Windows.Forms'
  250. 		$answer = [System.Windows.Forms.MessageBox]::Show( $message, $title, $buttons )
  251. 		if ( $answer -eq 'Yes' ) {
  252. 			$url = 'https://www.faa.gov/licenses_certificates/aircraft_certification/aircraft_registry/releasable_aircraft_download/'
  253. 			Start-Process $url
  254. 		} else {
  255. 			ShowHelp( "No downloaded FAA Aircraft Registry's Releasable Aircraft Database found, please download it and try again" )
  256. 		}
  257. 	}
  258. }
  259.  

page last uploaded: 2019-01-21, 22:48