Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregccmd.ps

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

  1. <#
  2. .SYNOPSIS
  3. Search a downloaded Canadian Civil Aircraft Register database for an aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
  4.  
  5. .DESCRIPTION
  6. First, create a subdirectory 'C' in this script's parent folder.
  7. Next, download the Canadian Civil Aircraft Register database (see links section), unzip it and move the file carscurr.txt (and optionally the other files as well) to the 'C' folder.
  8. Now run this script with an aircraft registration as its only parameter (see examples section).
  9. The script will first look for the specified registration code in column 1 of the carscurr.txt file.
  10. If a match is found, the manufacturer and model are found in columns 4 and 5.
  11. 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 './AirRegCCmd.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 Canadian aircraft registration, i.e. C-xxx or C-xxxx (where each 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. . ./AirRegCCmd.ps1 "C-AHA"
  34. Will return tab-delimited string "N1944S<tab>BOEING<tab>E75N1", and set variables $Manufacturer to "BOEING" and $Model to "E75N1"
  35.  
  36. .EXAMPLE
  37. "C-AHA" | . ./AirRegCCmd.ps1
  38. Will also return tab-delimited string "N1944S<tab>BOEING<tab>E75N1", and set variables $Manufacturer to "BOEING" and $Model to "E75N1"
  39.  
  40. .EXAMPLE
  41. . ./AirRegCCmd.ps1 "C-AHA" -Debug
  42. This will return:
  43.  
  44. Start searching AHA in carscurr.txt file at <date> <time>
  45. C-AHA   Boeing  E75N1
  46.  
  47. Finished at <date> <time> (elapsed time <time elapsed>)
  48.  
  49. .EXAMPLE
  50. Create and run the following PowerShell script:
  51. ===============================================================
  52. $Registration = 'C-AHA' ; $Manufacturer = '' ; $Model = ''
  53. [void] ( . "$PSScriptRoot\AirRegCCmd.ps1" -Registration $Registration )
  54. Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel        : {2}" -f $Registration, $Manufacturer, $Model )
  55. ===============================================================
  56.  
  57. Besides setting variables $Manufacturer to "BOEING" and $Model to "E75", it will return:
  58.  
  59. Registration : C-AHA
  60. Manufacturer : BOEING
  61. Model        : E75N1
  62.  
  63. .EXAMPLE
  64. Create and run the following batch file:
  65. ===============================================================
  66. REM Note that there should only be a TAB and nothing else between delims= and the doublequote
  67. FOR /F "tokens=1-3 delims=	" %%A IN ('powershell . ./AirRegCCmd.ps1 C-AHA') DO (
  68. 	ECHO Registration : %%A
  69. 	ECHO Manufacturer : %%B
  70. 	ECHO Model        : %%C
  71. )
  72. ===============================================================
  73.  
  74. It will return:
  75.  
  76. Registration : C-AHA
  77. Manufacturer : BOEING
  78. Model        : E75N1
  79.  
  80. .LINK
  81. Script written by Rob van der Woude:
  82. https://www.robvanderwoude.com/
  83.  
  84. .LINK
  85. Canadian Civil Aircraft Register database:
  86. https://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx
  87.  
  88. .LINK
  89. Capture -Debug parameter by mklement0 on StackOverflow.com:
  90. https://stackoverflow.com/a/48643616
  91. #>
  92.  
  93. param (
  94. 	[parameter( ValueFromPipeline )]
  95. 	[ValidatePattern("(^\s*$|[\?/-]|^C-[A-Z]{3,4}$)")]
  96. 	[string]$Registration,
  97. 	[switch]$Quiet,
  98. 	[switch]$Help
  99. )
  100.  
  101. $progver = "1.00"
  102.  
  103. $Registration = $Registration.ToUpper( )
  104. [string]$Manufacturer = ''
  105. [string]$Model = ''
  106. [bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  107.  
  108. function ShowHelp( $errormessage = '' ) {
  109. 	if ( !$Quiet ) {
  110. 		if ( $errormessage ) {
  111. 			Write-Host
  112. 			Write-Host "Error: " -ForegroundColor Red -NoNewline
  113. 			Write-Host $errormessage
  114. 		}
  115. 		Write-Host
  116. 		Write-Host ( "AirRegCCmd.ps1,  Version {0}" -f $progver )
  117. 		Write-Host "Search downloaded Canadian Civil Aircraft Register database for a registation"
  118. 		Write-Host
  119. 		Write-Host "Usage:  " -NoNewline
  120. 		Write-Host ". ./AirRegCCmd.ps1 [-Registration] C-*** [-Quiet] [-Debug] [-Help]" -ForegroundColor White
  121. 		Write-Host
  122. 		Write-Host "Where:   " -NoNewline
  123. 		Write-Host "C-***           " -NoNewline -ForegroundColor White
  124. 		Write-Host "is a valid Canadian aircraft registration, e.g. C-AHA"
  125. 		Write-Host "         -Quiet          " -NoNewline -ForegroundColor White
  126. 		Write-Host "all errors are ignored and no error messages displayed"
  127. 		Write-Host "         -Debug          " -NoNewline -ForegroundColor White
  128. 		Write-Host "shows some progress messages"
  129. 		Write-Host "         -Help           " -NoNewline -ForegroundColor White
  130. 		Write-Host "shows this help screen"
  131. 		Write-Host
  132. 		Write-Host "Notes:   This script requires a downloaded Canadian Civil Aircraft Register"
  133. 		Write-Host "         database, located in a subfolder 'C' of this script's parent folder."
  134. 		Write-Host "         The Canadian Civil Aircraft Register database can be downloaded at:"
  135. 		Write-Host "         https://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx" -ForegroundColor DarkGray
  136. 		Write-Host "         The result, if any, of the search is displayed as tab-delimited text:"
  137. 		Write-Host "         <registration><tab><manufacturer><tab><model>"
  138. 		Write-Host "         Besides its screen output, this script will also set the `$Manufacturer"
  139. 		Write-Host "         and `$Model variables with the database search result."
  140. 		Write-Host "         Run " -NoNewline
  141. 		Write-Host "Get-Help `"./AirRegCCmd.ps1`" -Examples " -NoNewline -ForegroundColor White
  142. 		Write-Host "for some examples of"
  143. 		Write-Host "         `"nesting`" this script in other PowerShell or batch scripts."
  144. 		Write-Host "         Return code (`"ErrorLevel`") 1 in case of errors, otherwise 0."
  145. 		Write-Host
  146. 		Write-Host "Credits: Code to capture -Debug parameter by mklement0 on StackOverflow.com:"
  147. 		Write-Host "         https://stackoverflow.com/a/48643616" -ForegroundColor DarkGray
  148. 		Write-Host
  149. 		Write-Host "Written by Rob van der Woude"
  150. 		Write-Host "https://www.robvanderwoude.com"
  151. 	}
  152. 	exit 1
  153. }
  154.  
  155. if ( $Help -or $Registration -match '(^\s*$|[\?/])' ) {
  156. 	ShowHelp
  157. 	exit 1
  158. }
  159.  
  160. $C_number = $Registration.Substring( 2 )
  161.  
  162. $dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'C' )
  163. $dbfile = ( Join-Path -Path $dbfolder -ChildPath 'carscurr.txt' )
  164.  
  165. if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
  166. 	if ( $Debug ) {
  167. 		$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
  168. 		Write-Host ( "Start searching {0} in carscurr.txt file at {1}" -f $C_number, ( Get-Date ) )
  169. 	}
  170. 	$pattern = '^\"{0,4}\",[^\n\r]+' -f $C_number
  171. 	$record = ( ( Get-Content -Path $dbfile ) -match $pattern )
  172. 	if ( $record ) {
  173. 		$Manufacturer = ( $record.Split( ',' )[3] -replace '(^\"|\s*\")','' )
  174. 		$Model = ( $record.Split( "," )[4] -replace '(^\"|\s*\")','' )
  175. 	}
  176. 	"{0}`t{1}`t{2}" -f $Registration.ToUpper( ), $Manufacturer, $Model | Out-String
  177. 	if ( $Debug ) {
  178. 		Write-Host ( "Finished at {0} (elapsed time {1})`n`n" -f ( Get-Date ), $StopWatch.Elapsed )
  179. 		$StopWatch.Stop( )
  180. 	}
  181. } else {
  182. 	if ( $Quiet ) {
  183. 		if ( $Debug ) {
  184. 			Write-Host ( "Downloaded Canadian Civil Aircraft Register database file `"{0}`" not found" -f $dbfile )
  185. 		}
  186. 		exit 1
  187. 	} else {
  188. 		$message = "No downloaded Canadian Civil Aircraft Register database was found.`n`nDo you want to open the download webpage for the database now?"
  189. 		$title   = 'No Database Found'
  190. 		$buttons = 'YesNo'
  191. 		Add-Type -AssemblyName System.Windows.Forms
  192. 		$answer = [System.Windows.Forms.MessageBox]::Show( $message, $title, $buttons )
  193. 		if ( $answer -eq "Yes" ) {
  194. 			$url = 'https://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx'
  195. 			Start-Process $url
  196. 		} else {
  197. 			ShowHelp( 'No downloaded Canadian Civil Aircraft Register database found, please download it and try again' )
  198. 		}
  199. 	}
  200. }
  201.  

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