Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregoocmd.ps

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

  1. <#
  2. .SYNOPSIS
  3. Search Pascal Brugier's online Belgian aircraft registration database for a Belgian aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
  4.  
  5. .DESCRIPTION
  6. Run this script with an aircraft registration as its only parameter (see examples section).
  7. The script will try to find the registration in Pascal Brugier's online Belgian aircraft registration database.
  8. If a match is found, the script will display a tab-delimited string with the registration and the manufacturer/model (<registration><tab><tab><manufacturer_and_model>).
  9. If the script was started by another PowerShell script, the calling PowerShell script may also read the manufacturer/model from the variable $Model, passed on by this script.
  10. 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/model.
  11. Get-Help './AirRegOOCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  12.  
  13. .PARAMETER Registration
  14. A valid Belgian aircraft registration, i.e. O-**** or OO-****
  15.  
  16. .PARAMETER Quiet
  17. Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1
  18.  
  19. .PARAMETER Help
  20. Show the script's help screen
  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 Debug
  26. Show some progress messages
  27.  
  28. .OUTPUTS
  29. A tab-delimited string with <registration><tab><tab><manufacturer_and_model>, and a variable $Model set to manufacturer AND model
  30.  
  31. .EXAMPLE
  32. . ./AirRegOOCmd.ps1 "OO-AHZ"
  33. Will return tab-delimited string "OO-AHZ<tab><tab>SABCA HANDLEY PAGE W.8F" and set variable $Model to "SABCA HANDLEY PAGE W.8F"
  34.  
  35. .EXAMPLE
  36. "OO-AHZ" | . ./AirRegOOCmd.ps1
  37. Will also return tab-delimited string "OO-AHZ<tab><tab>SABCA HANDLEY PAGE W.8F" and set variable $Model to "SABCA HANDLEY PAGE W.8F"
  38.  
  39. .EXAMPLE
  40. . ./AirRegOOCmd.ps1 -Version -Verbose
  41. Will return the full script path, version and last modified/release date
  42.  
  43. .EXAMPLE
  44. . ./AirRegOOCmd.ps1 -Version
  45. Will return the script version
  46.  
  47. .EXAMPLE
  48. Create and run the following PowerShell script:
  49. ===============================================================
  50. $Registration = 'OO-AHZ' ; $Model = ''
  51. [void] ( . "$PSScriptRoot\AirRegOOCmd.ps1" -Registration $Registration )
  52. Write-Host ( "Registration : {0}`nModel        : {1}" -f $Registration, $Model )
  53. ===============================================================
  54.  
  55. Besides setting variable $Model to "SABCA HANDLEY PAGE W.8F", it will return:
  56.  
  57. Registration : OO-AHZ
  58. Model        : SABCA HANDLEY PAGE W.8F
  59.  
  60. .EXAMPLE
  61. Create and run the following batch file:
  62. ===============================================================
  63. REM Note that there should only be a TAB and nothing else between delims= and the doublequote
  64. FOR /F "tokens=1-3 delims=	" %%A IN ('powershell . ./AirRegOOCmd.ps1 OO-AHZ') DO (
  65. 	ECHO Registration : %%A
  66. 	ECHO Model        : %%B
  67. )
  68. ===============================================================
  69.  
  70. It will return:
  71.  
  72. Registration : OO-AHZ
  73. Model        : SABCA HANDLEY PAGE W.8F
  74.  
  75. .LINK
  76. Script written by Rob van der Woude:
  77. https://www.robvanderwoude.com/
  78.  
  79. .LINK
  80. Pascal Brugier's online Belgian aircraft registration database:
  81. http://www.brugier.com/textes/txt/oo-aaa.txt
  82.  
  83. .LINK
  84. Capture -Debug and -Verbose parameter by mklement0 on StackOverflow.com:
  85. https://stackoverflow.com/a/48643616
  86. #>
  87.  
  88. param (
  89. 	[parameter( ValueFromPipeline )]
  90. 	[ValidatePattern("(^\s*$|[\?/]|^O{1,2}-[A-Z\d]{2,4}$)")]
  91. 	[string]$Registration,
  92. 	[switch]$Quiet,
  93. 	[switch]$Help,
  94. 	[switch]$Version
  95. )
  96.  
  97. $progver = "1.00"
  98.  
  99. $Registration = $Registration.ToUpper( )
  100. $Manufacturer = ''
  101. $Model = ''
  102. [bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  103. [bool]$Verbose = ( $PSBoundParameters.ContainsKey( 'Verbose' ) )
  104.  
  105. if ( $Version ) {
  106. 	if ( $Verbose ) {
  107. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  108. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  109. 			"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  110. 		} else {
  111. 			# if last modified time is not equal to program version, the script has been tampered with
  112. 			"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  113. 		}
  114. 	} else {
  115. 		$progver
  116. 	}
  117. 	exit 0
  118. }
  119.  
  120. function ShowHelp( $errormessage = '' ) {
  121. 	if ( !$Quiet ) {
  122. 		Clear-Host
  123. 		if ( $errormessage ) {
  124. 			Write-Host
  125. 			Write-Host "Error: " -ForegroundColor Red -NoNewline
  126. 			Write-Host $errormessage
  127. 		}
  128. 		Write-Host
  129. 		Write-Host ( "`"{0}`", Version {1}" -f $PSCommandPath, $progver ) -NoNewline
  130. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  131. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  132. 			Write-Host ", release date " -NoNewline
  133. 		} else {
  134. 			# if last modified time is not equal to program version, the script has been tampered with
  135. 			Write-Host ", last modified date " -NoNewline
  136. 		}
  137. 		Write-Host $lastmod.ToString( "yyyy-MM-dd" )
  138. 		Write-Host
  139. 		Get-Help $PSCommandPath -Full
  140. 	}
  141. }
  142.  
  143. if ( $Help -or $Registration -match '(^\s*$|[\?/])' ) {
  144. 	ShowHelp
  145. 	exit 1
  146. }
  147.  
  148. $dburl = 'http://www.brugier.com/textes/txt/oo-aaa.txt'
  149. $pattern = ( '^{0}\s[^\n\r]{{1}}' -f $Registration, ( 60 - $Registration.Length ).ToString( ) )
  150. if ( $Debug ) {
  151. 	$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
  152. 	Write-Host ( "Starting online search for {0} at {1}" -f $Registration, ( Get-Date ) )
  153. }
  154. $ProgressPreference = 'SilentlyContinue'
  155. $table = ( Invoke-WebRequest -Uri $dburl ).Content
  156. $ProgressPreference = 'Continue'
  157. if ( $table.Length -lt 100 ) {
  158. 	if ( !$Quiet ) {
  159. 		Write-Host "Error retrieving Belgian aircraft registration list"
  160. 	}
  161. 	exit 1
  162. }
  163. $linescount = $table.Split( [environment]::NewLine ).Length
  164. if ( $Debug ) {
  165. 	Write-Host ( "Registration list retrieved, {0} lines" -f $linescount )
  166. }
  167. if ( $linescount -lt 1000 ) {
  168. 	if ( !$Quiet ) {
  169. 		Write-Host ( "Error in retrieved Belgian aircraft registration list: only {0} lines" -f $linescount )
  170. 	}
  171. 	exit 1
  172. }
  173. $matchfound = $false
  174. $linescount = 0
  175. $table.Split( [environment]::NewLine ) | ForEach-Object {
  176. 	if ( !$matchfound ) {
  177. 		if ( $Model -ne '' ) {
  178. 			$matchfound = $true
  179. 			if ( $Debug ) {
  180. 				Write-Host ( "Match found in line {0}" -f $linescount )
  181. 			}
  182. 		}
  183. 		if ( $_.StartsWith( "$Registration " ) ) {
  184. 			$Model = $_.Substring( $Registration.Length ).Trim( )
  185. 			$pattern = "\s{2,}.*$"
  186. 			$Model = [System.Text.RegularExpressions.Regex]::Replace( $Model, $pattern, "" )
  187. 		}
  188. 		$linescount += 1
  189. 	}
  190. }
  191. if ( $Debug ) {
  192. 	Write-Host ( "Ended online search for {0} at {1} (elapsed time {2})" -f $Registration, ( Get-Date ), $StopWatch.Elapsed )
  193. 	$StopWatch.Stop( )
  194. }
  195. if ( !$matchfound ) {
  196. 	if ( !$Quiet ) {
  197. 		Write-Host "Aircraft registration for $Registration not found"
  198. 	}
  199. } else {
  200. 	"{0}`t`t{1}" -f $Registration, $Model
  201. }
  202.  

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