Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregtfcmd.ps

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

  1. <#
  2. .SYNOPSIS
  3. Search the online Icelandic aircraft registration database for an Icelandic 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 the online Icelandic aircraft registry.
  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 $Manufacturer, 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 './AirRegPHCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  12.  
  13. .PARAMETER Registration
  14. A valid Icelandic aircraft registration, i.e. TF-xxx (where each x is a single letter)
  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 Debug
  23. Show some progress messages
  24.  
  25. .OUTPUTS
  26. A tab-delimited string with <registration><tab><manufacturer_and_model>, and a variable $Model set to manufacturer AND model
  27.  
  28. .EXAMPLE
  29. . ./AirRegTFmd.ps1 "TF-NPK"
  30. Will return tab-delimited string "TF-NPK<tab>Douglas C47A" and set variable $Model to "Douglas C47A"
  31.  
  32. .EXAMPLE
  33. "TF-NPK" | . ./AirRegTFCmd.ps1
  34. Will also return tab-delimited string "TF-NPK<tab>Douglas C47A" and set variable $Model to "Douglas C47A"
  35.  
  36. .EXAMPLE
  37. Create and run the following PowerShell script:
  38. ===============================================================
  39. $Registration = 'TF-NPK' ; $Model = ''
  40. [void] ( . "$PSScriptRoot\AirRegTFCmd.ps1" -Registration $Registration )
  41. Write-Host ( "Registration : {0}`nModel        : {1}" -f $Registration, $Model )
  42. ===============================================================
  43.  
  44. Besides setting variable $Model to "Douglas C47A", it will return:
  45.  
  46. Registration : TF-NPK
  47. Model        : Douglas C47A
  48.  
  49. .EXAMPLE
  50. Create and run the following batch file:
  51. ===============================================================
  52. REM Note that there should only be a TAB and nothing else between delims= and the doublequote
  53. FOR /F "tokens=1-3 delims=	" %%A IN ('powershell . ./AirRegTFCmd.ps1 TF-NPK') DO (
  54. 	ECHO Registration : %%A
  55. 	ECHO Model        : %%B
  56. )
  57. ===============================================================
  58.  
  59. It will return:
  60.  
  61. Registration : TF-NPK
  62. Model        : Douglas C47A
  63.  
  64. .LINK
  65. Script written by Rob van der Woude:
  66. https://www.robvanderwoude.com/
  67.  
  68. .LINK
  69. Icelandic Transport Authority online aircraft registry:
  70. https://www.icetra.is/aviation/aircraft/register/
  71. #>
  72.  
  73. param (
  74. 	[parameter( ValueFromPipeline )]
  75. 	[ValidatePattern("(^\s*$|[\?/]|^TF-[A-Z]{3}$)")]
  76. 	[string]$Registration,
  77. 	[switch]$Quiet,
  78. 	[switch]$Help
  79. )
  80.  
  81. $progver = "1.00"
  82.  
  83. $Registration = $Registration.ToUpper( )
  84. $Manufacturer = ''
  85. $Model = ''
  86. [bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  87.  
  88. function ShowHelp( $message = '' ) {
  89. 	if ( !$Quiet ) {
  90. 		if ( $errormessage ) {
  91. 			Write-Host
  92. 			Write-Host "Error: " -ForegroundColor Red -NoNewline
  93. 			Write-Host $errormessage
  94. 		}
  95. 		Write-Host
  96. 		Write-Host ( "AirRegTFCmd.ps1,  Version {0}" -f $progver )
  97. 		Write-Host "Search online Icelandic aircraft registration database for a registation"
  98. 		Write-Host
  99. 		Write-Host "Usage:  " -NoNewline
  100. 		Write-Host ". ./AirRegTFCmd.ps1 [-Registration] TF-*** [-Quiet] [-Debug] [-Help]" -ForegroundColor White
  101. 		Write-Host
  102. 		Write-Host "Where:  " -NoNewline
  103. 		Write-Host "TF-***           " -NoNewline -ForegroundColor White
  104. 		Write-Host "is a valid Icelandic aircraft registration, e.g. TF-NPK"
  105. 		Write-Host "         -Quiet          " -NoNewline -ForegroundColor White
  106. 		Write-Host "all errors are ignored and no error messages displayed"
  107. 		Write-Host "         -Debug          " -NoNewline -ForegroundColor White
  108. 		Write-Host "shows some progress messages"
  109. 		Write-Host "         -Help           " -NoNewline -ForegroundColor White
  110. 		Write-Host "shows this help screen"
  111. 		Write-Host
  112. 		Write-Host "Notes:  This script uses the Icelandic aircraft registration database at:"
  113. 		Write-Host "        https://www.icetra.is/aviation/aircraft/register/" -ForegroundColor DarkGray
  114. 		Write-Host "        The result, if any, of the search is displayed as tab-delimited text:"
  115. 		Write-Host "        <registration><tab><manufacturer_and_model>"
  116. 		Write-Host "        Besides its screen output, this script will also set the `$Model"
  117. 		Write-Host "        variable with the database search result."
  118. 		Write-Host "        Run " -NoNewline
  119. 		Write-Host "Get-Help `"./AirRegTFCmd.ps1`" -Examples " -NoNewline -ForegroundColor White
  120. 		Write-Host "for some examples of"
  121. 		Write-Host "        `"nesting`" this script in other PowerShell or batch scripts."
  122. 		Write-Host "        Return code (`"ErrorLevel`") 1 in case of errors, otherwise 0."
  123. 		Write-Host
  124. 		Write-Host "Written by Rob van der Woude"
  125. 		Write-Host "https://www.robvanderwoude.com"
  126. 	}
  127. 	exit 1
  128. }
  129.  
  130. if ( $Help -or $Registration -match "(^\s*$|^-|\?|/)" ) {
  131. 	ShowHelp
  132. 	exit 1
  133. }
  134.  
  135.  
  136. $dburl = 'https://www.icetra.is/aviation/aircraft/register/'
  137. #$regurl = "https://www.icetra.is/aviation/aircraft/register?aq=$Registration"
  138. $pattern = ( '(?sm)<td class="letters"><a[^>]*>({0})</a></td>[^<]*<td class="serialno">[^<]*</td>[^<]*<td class="type">([^<]+)</td>' -f $Registration )
  139. if ( $Debug ) {
  140. 	$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
  141. 	Write-Host ( "Starting online search for {0} at {1}" -f $Registration, ( Get-Date ) )
  142. }
  143. $ProgressPreference = 'SilentlyContinue'
  144. $html = ( Invoke-WebRequest -Uri $dburl -Method 'Merge' ).Content
  145. $ProgressPreference = 'Continue'
  146. $Matches = ( $html | Select-String -Pattern $pattern -AllMatches ).Matches
  147. if ( $Debug ) {
  148. 	$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
  149. 	Write-Host ( "Ended online search for {0} at {1} with {2} matches found (elapsed time {3})" -f $Registration, ( Get-Date ), $Matches.Count, $StopWatch.Elapsed )
  150. 	$StopWatch.Stop( )
  151. }
  152. if ( $Matches.Count -eq 0 ) {
  153. 	if ( !$Quiet ) {
  154. 		Write-Host "Aircraft registration for $Registration not found"
  155. 	}
  156. } else {
  157. 	$Model = $Matches[0].Groups[2]
  158. 	"{0}`t{1}" -f $Matches[0].Groups[1], $Model
  159. }
  160.  

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