Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airreghbcmd.ps

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

  1. <#
  2. .SYNOPSIS
  3. Search a downloaded Swiss aircraft registry database for a Swiss aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
  4.  
  5. .DESCRIPTION
  6. First, create a subdirectory 'HB' in this script's parent folder.
  7. Next, download the Swiss aircraft registry database in CSV format (see links section), and move the file 'Swiss Aircraft Register.csv' to the 'HB' 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 2 of the 'Swiss Aircraft Register.csv' file.
  10. If a match is found, the manufacturer and model are found in columns 6 and 7.
  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 './AirRegHBCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  15.  
  16. .PARAMETER Registration
  17. A valid Swiss aircraft registration, e.g. HB-1044
  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. . ./AirRegHBCmd.ps1 HB-1044
  40. Will return tab-delimited string "HB-1044<tab>SEGELFLUGZEUGBAU A. NEUKOM<tab>AN 66 C", and set variables $Manufacturer to "HB-1044	SEGELFLUGZEUGBAU A. NEUKOM" and $Model to "AN 66 C"
  41.  
  42. .EXAMPLE
  43. "HB-1044" | . ./AirRegHBCmd.ps1
  44. Will also return tab-delimited string HB-1044<tab>SEGELFLUGZEUGBAU A. NEUKOM<tab>AN 66 C", and set variables $Manufacturer to "HB-1044	SEGELFLUGZEUGBAU A. NEUKOM" and $Model to "AN 66 C"
  45.  
  46. .EXAMPLE
  47. . ./AirRegHBCmd.ps1 "HB-1044" -Debug
  48. This will return:
  49.  
  50. Start searching "HB-1044" in "Swiss Aircraft Register.csv" file at <date> <time>
  51. Found a match at 2021-08-17 15:13:59
  52. HB-1044	SEGELFLUGZEUGBAU A. NEUKOM	AN 66 C
  53.  
  54. Finished at <date> <time> (elapsed time <time elapsed>)
  55.  
  56. .EXAMPLE
  57. Create and run the following PowerShell script:
  58. ===============================================================
  59. $Registration = 'HB-1044' ; $Manufacturer = '' ; $Model = ''
  60. [void] ( . "$PSScriptRoot\AirRegHBCmd.ps1" -Registration $Registration )
  61. Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel        : {2}" -f $Registration, $Manufacturer, $Model )
  62. ===============================================================
  63.  
  64. Besides setting variables $Manufacturer to "THB-1044	SEGELFLUGZEUGBAU A. NEUKOM" and $Model to "AN 66 C", it will return:
  65.  
  66. Registration : HB-1044
  67. Manufacturer : HB-1044	SEGELFLUGZEUGBAU A. NEUKOM
  68. Model        : AN 66 C
  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 . ./AirRegHBCmd.ps1 HB-1044') DO (
  75. 	ECHO Registration : %%A
  76. 	ECHO Manufacturer : %%B
  77. 	ECHO Model        : %%C
  78. )
  79. ===============================================================
  80.  
  81. It will return:
  82.  
  83. Registration : HB-1044
  84. Manufacturer : HB-1044	SEGELFLUGZEUGBAU A. NEUKOM
  85. Model        : AN 66 C
  86.  
  87. .LINK
  88. Script written by Rob van der Woude:
  89. https://www.robvanderwoude.com/
  90.  
  91. .LINK
  92. Swiss Aircraft Register:
  93. https://app02.bazl.admin.ch/web/bazl/en/#/lfr/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*$|[\?/]|^-|^HB-([A-Z]{3}|\d{2,4})$)")]
  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 'HB' )
  134. $dbfile = ( Join-Path -Path $dbfolder -ChildPath 'Swiss Aircraft Register.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. if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
  163. 	if ( $Debug ) {
  164. 		$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
  165. 		Write-Host ( "Start searching `"{0}`" in `"Swiss Aircraft Register.csv`" file at {1}" -f $Registration, ( Get-Date ) )
  166. 	}
  167.     $delimiter = ';'
  168.     $pattern = "^`"\d+`"{0}`"{1}`"{0}`"[^\n\r]+" -f $delimiter, $Registration
  169. 	$record = ( ( Get-Content -Path $dbfile ) -match $pattern )
  170. 	if ( $record ) {
  171.         if ( $record.Split( $delimiter ).Count -gt 6 ) {
  172. 			$Manufacturer = $record.Split( $delimiter )[5] -replace '"',''
  173. 			$Model = $record.Split( $delimiter )[6] -replace '"',''
  174. 			if ( $Debug ) {
  175. 				Write-Host ( "Found a match at {0}" -f ( Get-Date ) )
  176. 			}
  177. 		}
  178. 	}
  179. 	"{0}`t{1}`t{2}" -f $Registration, $Manufacturer, $Model | Out-String
  180. 	if ( $Debug ) {
  181. 		Write-Host ( "Finished at {0} (elapsed time {1})`n`n" -f ( Get-Date ), $StopWatch.Elapsed )
  182. 		$StopWatch.Stop( )
  183. 	}
  184. } else {
  185. 	if ( $Quiet ) {
  186. 		if ( $Debug ) {
  187. 			Write-Host ( "Downloaded Swiss aircraft registry database file `"{0}`" not found" -f $dbfile )
  188. 		}
  189. 		exit 1
  190. 	} else {
  191. 		$message = "No downloaded Swiss aircraft registry database was found.`n`nDo you want to open the download webpage for the database now?"
  192. 		$title   = 'No Database Found'
  193. 		$buttons = 'YesNo'
  194. 		Add-Type -AssemblyName System.Windows.Forms
  195. 		$answer = [System.Windows.Forms.MessageBox]::Show( $message, $title, $buttons )
  196. 		if ( $answer -eq "Yes" ) {
  197. 			$url = 'https://app02.bazl.admin.ch/web/bazl/en/#/lfr/search'
  198. 			Start-Process $url
  199. 		} else {
  200. 			ShowHelp( 'No downloaded Swiss aircraft registry database found, please download it and try again' )
  201. 		}
  202. 	}
  203. }
  204.  

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