Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregzkcmd.ps

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

  1. <#
  2. .SYNOPSIS
  3. Search a downloaded New Zealand aircraft registry database for an New Zealand 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 New Zealand aircraft registry database (see links section), unZIP and move the file AircraftRegisterExport.tab to the 'ZK' folder.
  8. Now run this script with an New Zealand aircraft registration as its only parameter (see examples section).
  9. The script will first look for the word 'Aeroplane' in column 1 and the specified registration code in column 2 of the tab-delimited AircraftRegisterExport.tab file.
  10. If a match is found, the manufacturer and model are found in columns 3 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 './AirRegZKCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  15.  
  16. .PARAMETER Registration
  17. A valid NewZealand aircraft registration, e.g. ZK-ADI
  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. . ./AirRegZKCmd.ps1 ZK-ADI
  40. Will return tab-delimited string "ZK-ADI<tab>De Havilland<tab>DH 83 Fox Moth", and set variables $Manufacturer to "De Havilland" and $Model to "DH 83 Fox Moth"
  41.  
  42. .EXAMPLE
  43. "ZK-ADI" | . ./AirRegZKCmd.ps1
  44. Will also return tab-delimited string "ZK-ADI<tab>De Havilland<tab>DH 83 Fox Moth", and set variables $Manufacturer to "De Havilland" and $Model to "DH 83 Fox Moth"
  45.  
  46. .EXAMPLE
  47. . ./AirRegZKCmd.ps1 "ZK-ADI" -Debug
  48. This will return:
  49.  
  50. Start searching "ADI" in AircraftRegisterExport.tab at <date> <time>
  51. Found a match at <date> <time>
  52. ZK-ADI  De Havilland    DH 83 Fox Moth
  53.  
  54. Finished at <date> <time> (elapsed time <time elapsed>)
  55.  
  56. .EXAMPLE
  57. Create and run the following PowerShell script:
  58. ===============================================================
  59. $Registration = ZK-ADI' ; $Manufacturer = '' ; $Model = ''
  60. [void] ( . "$PSScriptRoot\AirRegZKCmd.ps1" -Registration $Registration )
  61. Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel        : {2}" -f $Registration, $Manufacturer, $Model )
  62. ===============================================================
  63.  
  64. Besides setting variables $Manufacturer to "De Havilland" and $Model to "DH 83 Fox Moth", it will return:
  65.  
  66. Registration : ZK-ADI
  67. Manufacturer : De Havilland
  68. Model        : DH 83 Fox Moth
  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 . ./AirRegZKCmd.ps1 ZK-ADI') DO (
  75. 	ECHO Registration : %%A
  76. 	ECHO Manufacturer : %%B
  77. 	ECHO Model        : %%C
  78. )
  79. ===============================================================
  80.  
  81. It will return:
  82.  
  83. Registration : ZK-ADI
  84. Manufacturer : De Havilland
  85. Model        : DH 83 Fox Moth
  86.  
  87. .LINK
  88. Script written by Rob van der Woude:
  89. https://www.robvanderwoude.com/
  90.  
  91. .LINK
  92. New Zealand Civil Aviation Authority, Aviation Security Service's aircraft registry database:
  93. https://www.aviation.govt.nz/aircraft/aircraft-registration/aircraft-register-search/
  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*$|[\?/]|^-|^ZK-[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 'ZK' )
  134. $dbfile = ( Join-Path -Path $dbfolder -ChildPath 'AircraftRegisterExport.tab' )
  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. $ZK_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 AircraftRegisterExport.tab at {1}" -f $ZK_num, ( Get-Date ) )
  168. 	}
  169. 	$pattern = "^Aeroplane`t{0}`t[^\n\r]+" -f $ZK_num
  170. 	$record = ( ( Get-Content -Path $dbfile ) -match $pattern )
  171. 	if ( $record ) {
  172. 		if ( $record.Split( "`t" ).Count -gt 3 ) {
  173. 			$Manufacturer = $record.Split( "`t" )[2]
  174. 			$Model = $record.Split( "`t" )[3]
  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 New Zealand aircraft registry database file `"{0}`" not found" -f $dbfile )
  189. 		}
  190. 		exit 1
  191. 	} else {
  192. 		$message = "No downloaded New Zealand 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.aviation.govt.nz/aircraft/aircraft-registration/aircraft-register-search/'
  199. 			Start-Process $url
  200. 		} else {
  201. 			ShowHelp( 'No downloaded New Zealand aircraft registry database found, please download it and try again' )
  202. 		}
  203. 	}
  204. }
  205.  

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