Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregfcmd.ps

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

  1. <#
  2. .SYNOPSIS
  3. Search a downloaded French Agence Nationale des Titres Sécurisés aircraft registry database for a French aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
  4.  
  5. .DESCRIPTION
  6. First, create a subdirectory 'F' in this script's parent folder.
  7. Next, download the Agence Nationale des Titres Sécurisés aircraft registry database (see links section, use link marked 'Données du registre' on the website), move the file export.csv to the 'F' folder.
  8. Now run this script with a French 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 export.csv file.
  10. If a match is found, the manufacturer and model are found in columns 2 and 3.
  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 './AirRegFCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  15.  
  16. .PARAMETER Registration
  17. A valid French aircraft registration, e.g. F-AZXN
  18.  
  19. .PARAMETER Quiet
  20. Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1.
  21.  
  22. .PARAMETER Version
  23. Show this script's version number; if combined with -Verbose show full script path, version number and last/modified/release date
  24.  
  25. .PARAMETER CheckDB
  26. If the required local database can be found, returns True and exit code 0; if not, returns False and exit code 1.
  27.  
  28. .PARAMETER Help
  29. Show the script's help screen
  30.  
  31. .PARAMETER Debug
  32. Show some progress messages
  33.  
  34. .OUTPUTS
  35. 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
  36. If no match is found: False
  37.  
  38. .EXAMPLE
  39. . ./AirRegFCmd.ps1 F-AZXN
  40. Will return tab-delimited string "F-AZXN<tab>THE BOEING COMPANY<tab>STEARMAN A 75/N1", and set variables $Manufacturer to "THE BOEING COMPANY" and $Model to "STEARMAN A 75/N1"
  41.  
  42. .EXAMPLE
  43. "F-AZXN" | . ./AirRegFCmd.ps1
  44. Will also return tab-delimited string "F-AZXN<tab>THE BOEING COMPANY<tab>STEARMAN A 75/N1", and set variables $Manufacturer to "THE BOEING COMPANY" and $Model to "STEARMAN A 75/N1"
  45.  
  46. .EXAMPLE
  47. . ./AirRegFCmd.ps1 "F-AZXN" -Debug
  48. This will return:
  49.  
  50. Start searching F-AZXN in export.csv file at <date> <time>
  51. F-AZXN  THE BOEING COMPANY      STEARMAN A 75/N1
  52.  
  53. Finished at <date> <time> (elapsed time <time elapsed>)
  54.  
  55. .EXAMPLE
  56. Create and run the following PowerShell script:
  57. ===============================================================
  58. $Registration = 'F-AZXN' ; $Manufacturer = '' ; $Model = ''
  59. [void] ( . "$PSScriptRoot\AirRegFCmd.ps1" -Registration $Registration )
  60. Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel        : {2}" -f $Registration, $Manufacturer, $Model )
  61. ===============================================================
  62.  
  63. Besides setting variables $Manufacturer to "THE BOEING COMPANY" and $Model to "STEARMAN A 75/N1", it will return:
  64.  
  65. Registration : F-AZXN
  66. Manufacturer : THE BOEING COMPANY
  67. Model        : STEARMAN A 75/N1
  68.  
  69. .EXAMPLE
  70. Create and run the following batch file:
  71. ===============================================================
  72. REM Note that there should only be a TAB and nothing else between delims= and the doublequote
  73. FOR /F "tokens=1-3 delims=	" %%A IN ('powershell . ./AirRegFCmd.ps1 F-AZXN') DO (
  74. 	ECHO Registration : %%A
  75. 	ECHO Manufacturer : %%B
  76. 	ECHO Model        : %%C
  77. )
  78. ===============================================================
  79.  
  80. It will return:
  81.  
  82. Registration : F-AZXN
  83. Manufacturer : THE BOEING COMPANY
  84. Model        : STEARMAN A 75/N1
  85.  
  86. .LINK
  87. Script written by Rob van der Woude:
  88. https://www.robvanderwoude.com/
  89.  
  90. .LINK
  91. Agence Nationale des Titres Sécurisés aircraft registry database:
  92. https://immat.aviation-civile.gouv.fr/immat/servlet/aeronef_liste.html
  93.  
  94. .LINK
  95. Capture -Debug parameter by mklement0 on StackOverflow.com:
  96. https://stackoverflow.com/a/48643616
  97. #>
  98.  
  99. param (
  100. 	[parameter( ValueFromPipeline )]
  101. 	[ValidatePattern("(^\s*$|[\?/]|^-|^F-[A-Z]{4}$)")]
  102. 	[string]$Registration,
  103. 	[switch]$CheckDB,
  104. 	[switch]$Version,
  105. 	[switch]$Quiet,
  106. 	[switch]$Help
  107. )
  108.  
  109. $progver = "1.00"
  110.  
  111. $Registration = $Registration.ToUpper( )
  112. $Manufacturer = ''
  113. $Model = ''
  114. [bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  115. [bool]$Verbose = ( $PSBoundParameters.ContainsKey( 'Verbose' ) )
  116.  
  117. if ( $Version ) {
  118. 	if ( $Verbose ) {
  119. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  120. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  121. 			"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  122. 		} else {
  123. 			# if last modified time is not equal to program version, the script has been tampered with
  124. 			"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  125. 		}
  126. 	} else {
  127. 		$progver
  128. 	}
  129. 	exit 0
  130. }
  131.  
  132. if ( $Help -or [string]::IsNullOrWhiteSpace( $Registration ) -or ( $Registration -match "[/\?]" ) ) {
  133. 	Clear-Host
  134. 	Write-Host ( "`"{0}`", Version {1}" -f $PSCommandPath, $progver ) -NoNewline
  135. 	$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  136. 	if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  137. 		Write-Host ", release date " -NoNewline
  138. 	} else {
  139. 		# if last modified time is not equal to program version, the script has been tampered with
  140. 		Write-Host ", last modified date " -NoNewline
  141. 	}
  142. 	Write-Host $lastmod.ToString( "yyyy-MM-dd" )
  143. 	Write-Host
  144. 	Get-Help $PSCommandPath -Full
  145. 	exit -1
  146. }
  147.  
  148. $dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'F' )
  149. $dbfile = ( Join-Path -Path $dbfolder -ChildPath 'export.csv' )
  150.  
  151. if ( $CheckDB ) {
  152. 	if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
  153. 		[bool]$true
  154. 		exit 0
  155. 	} else {
  156. 		[bool]$false
  157. 		exit 1
  158. 	}
  159. }
  160.  
  161. if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
  162. 	if ( $Debug ) {
  163. 		$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
  164. 		Write-Host ( "Start searching {0} in export.csv file at {1}" -f $Registration, ( Get-Date ) )
  165. 	}
  166. 	$pattern = '^{0};[^\n\r]+' -f $Registration
  167. 	$record = ( ( Get-Content -Path $dbfile ) -match $pattern )
  168. 	if ( $record ) {
  169. 		if ( $record.Split( ';' ).Count -gt 2 ) {
  170. 			$Manufacturer = $record.Split( ';' )[1]
  171. 			$Model = $record.Split( ';' )[2]
  172. 		}
  173. 	}
  174. 	"{0}`t{1}`t{2}" -f $Registration, $Manufacturer, $Model | Out-String
  175. 	if ( $Debug ) {
  176. 		Write-Host ( "Finished at {0} (elapsed time {1})`n`n" -f ( Get-Date ), $StopWatch.Elapsed )
  177. 		$StopWatch.Stop( )
  178. 	}
  179. } else {
  180. 	if ( $Quiet ) {
  181. 		if ( $Debug ) {
  182. 			Write-Host ( "Downloaded French Agence Nationale des Titres Sécurisés aircraft registry database file `"{0}`" not found" -f $dbfile )
  183. 		}
  184. 		exit 1
  185. 	} else {
  186. 		$message = "No downloaded French Agence Nationale des Titres Sécurisés aircraft registry database was found.`n`nDo you want to open the download webpage for the database now?"
  187. 		$title   = 'No Database Found'
  188. 		$buttons = 'YesNo'
  189. 		Add-Type -AssemblyName System.Windows.Forms
  190. 		$answer = [System.Windows.Forms.MessageBox]::Show( $message, $title, $buttons )
  191. 		if ( $answer -eq "Yes" ) {
  192. 			$url = 'https://immat.aviation-civile.gouv.fr/immat/servlet/aeronef_liste.html'
  193. 			Start-Process $url
  194. 		} else {
  195. 			ShowHelp( 'No downloaded French Agence Nationale des Titres Sécurisés aircraft registry database found, please download it and try again' )
  196. 		}
  197. 	}
  198. }
  199.  

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