Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregvhcmd.ps

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

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

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