Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregpucmd.ps

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

  1. <#
  2. .SYNOPSIS
  3. Search the online Brasilian Agência Nacional De Ação Civil aircraft registration database for an Brasilian aircraft registation (PPxxx..PUxxx) 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 Brasilian Agência Nacional De Ação Civil aircraft registry.
  8. If a match is found, the script will display a tab-delimited string with the registration, manufacturer and model (<registration><tab><manufacturer><tab><model>).
  9. To use this script as an extension for AirRegGUI.ps1, it needs to be copied for each Brasilian registration prefix used, i.e. AiRegPPCmd.ps1 needs to be copied to AirRegPRCmd.ps1 and AirRegPSCmd.ps1 and AirRegPTCmd.ps1 and AirRegPUCmd.ps1, and the help text and examples need to reflect the actual file names of the copied scripts.
  10. 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.
  11. 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.
  12. Get-Help './AirRegPUCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  13.  
  14. .PARAMETER Registration
  15. A valid Brasilian aircraft registration, i.e. PPxxx or PRxxx .. PUxxx (where each x is a single letter, no dash)
  16.  
  17. .PARAMETER Quiet
  18. Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1
  19.  
  20. .PARAMETER Help
  21. Show the script's help screen
  22.  
  23. .PARAMETER Version
  24. Show this script's version number; if combined with -Verbose show full script path, version number and last/modified/release date
  25.  
  26. .PARAMETER Debug
  27. Show some progress messages
  28.  
  29. .OUTPUTS
  30. A tab-delimited string with <registration><tab><manufacturer><tab><model>, and variable $Manufacturer and $Model set to manufacturer and model
  31.  
  32. .EXAMPLE
  33. . ./AirRegPUCmd.ps1 "PRATX"
  34. Will return tab-delimited string "PRATX<tab>CESSNA AIRCRAFT<tab>A188B" and set variables $Manufacturer to "CESSNA AIRCRAFT" and $Model to "A188B"
  35.  
  36. .EXAMPLE
  37. "PRATX" | . ./AirRegPUCmd.ps1
  38. Will also return tab-delimited string "PRATX<tab>CESSNA AIRCRAFT<tab>A188B" and set variables $Manufacturer to "CESSNA AIRCRAFT" and $Model to "A188B"
  39.  
  40. .EXAMPLE
  41. . ./AirRegPUCmd.ps1 -Version -Verbose
  42. Will return the full script path, version and last modified/release date
  43.  
  44. .EXAMPLE
  45. . ./AirRegPUCmd.ps1 -Version
  46. Will return the script version
  47.  
  48. .EXAMPLE
  49. Create and run the following PowerShell script:
  50. ===============================================================
  51. $Registration = 'PRATX' ; $Model = ''
  52. [void] ( . "$PSScriptRoot\AirRegPUCmd.ps1" -Registration $Registration )
  53. Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel        : {2}" -f $Registration, $Manufacturer, $Model )
  54. ===============================================================
  55.  
  56. Besides setting variables $Manufacturer to "CESSNA AIRCRAFT" and $Model to "A188B", it will return:
  57.  
  58. Registration : PRATX
  59. Manufacturer : CESSNA AIRCRAFT
  60. Model        : A188B
  61.  
  62. .EXAMPLE
  63. Create and run the following batch file:
  64. ===============================================================
  65. REM Note that there should only be a TAB and nothing else between delims= and the doublequote
  66. FOR /F "tokens=1-3 delims=	" %%A IN ('powershell . ./AirRegPUCmd.ps1 PRATX') DO (
  67. 	ECHO Registration : %%A
  68. 	ECHO Manufacturer : %%B
  69. 	ECHO Model        : %%C
  70. )
  71. ===============================================================
  72.  
  73. It will return:
  74.  
  75. Registration : PRATX
  76. Manufacturer : CESSNA AIRCRAFT
  77. Model        : A188B
  78.  
  79. .LINK
  80. Script written by Rob van der Woude:
  81. https://www.robvanderwoude.com/
  82.  
  83. .LINK
  84. Brasilian Agência Nacional De Ação Civil online aircraft registration database:
  85. https://sistemas.anac.gov.br/aeronaves/cons_rab_en.asp
  86.  
  87. .LINK
  88. Capture -Debug and -Verbose parameter by mklement0 on StackOverflow.com:
  89. https://stackoverflow.com/a/48643616
  90. #>
  91.  
  92. param (
  93. 	[parameter( ValueFromPipeline )]
  94. 	[ValidatePattern("(^\s*$|[\?/]|^P[PRSTU][A-Z]{3}$)")]
  95. 	[string]$Registration,
  96. 	[switch]$Quiet,
  97. 	[switch]$Help,
  98. 	[switch]$Version
  99. )
  100.  
  101. $progver = "1.01"
  102.  
  103. $Registration = $Registration.ToUpper( )
  104. $Manufacturer = ''
  105. $Model = ''
  106. [bool]$Debug = $Debug -or ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  107. [bool]$Verbose = ( $PSBoundParameters.ContainsKey( 'Verbose' ) )
  108.  
  109. if ( $Version ) {
  110. 	if ( $Verbose ) {
  111. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  112. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  113. 			"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  114. 		} else {
  115. 			# if last modified time is not equal to program version, the script has been tampered with
  116. 			"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  117. 		}
  118. 	} else {
  119. 		$progver
  120. 	}
  121. 	exit 0
  122. }
  123.  
  124. function ShowHelp( $message = '' ) {
  125. 	if ( !$Quiet ) {
  126. 		if ( $errormessage ) {
  127. 			Write-Host
  128. 			Write-Host "Error: " -ForegroundColor Red -NoNewline
  129. 			Write-Host $errormessage
  130. 		}
  131. 		Write-Host
  132. 		Write-Host ( "AirRegPUCmd.ps1,  Version {0}" -f $progver )
  133. 		Write-Host "Search online Brasilian aircraft registration database for a registation"
  134. 		Write-Host
  135. 		Write-Host "Usage:  " -NoNewline
  136. 		Write-Host ". ./AirRegPUCmd.ps1 [-Registration] PU*** [-Quiet] [-Debug] [-Help]" -ForegroundColor White
  137. 		Write-Host
  138. 		Write-Host "Where:  " -NoNewline
  139. 		Write-Host "PU***           " -NoNewline -ForegroundColor White
  140. 		Write-Host "is a valid Brasilian aircraft registration, e.g. PUAIA"
  141. 		Write-Host "        -Quiet          " -NoNewline -ForegroundColor White
  142. 		Write-Host "all errors are ignored and no error messages displayed"
  143. 		Write-Host "        -Debug          " -NoNewline -ForegroundColor White
  144. 		Write-Host "shows some progress messages"
  145. 		Write-Host "        -Help           " -NoNewline -ForegroundColor White
  146. 		Write-Host "shows this help screen"
  147. 		Write-Host
  148. 		Write-Host "Notes:  This script uses the Brasilian Agência Nacional De Ação Civil aircraft"
  149. 		Write-Host "        registration database at:"
  150. 		Write-Host "        https://sistemas.anac.gov.br/aeronaves/cons_rab_en.asp" -ForegroundColor DarkGray
  151. 		Write-Host "        The result, if any, of the search is displayed as tab-delimited text:"
  152. 		Write-Host "        <registration><tab><manufacturer><tab><model>"
  153. 		Write-Host "        Besides its screen output, this script will also set the `SManufacturer"
  154. 		Write-Host "        and `$Model variables with the database search result."
  155. 		Write-Host "        Run " -NoNewline
  156. 		Write-Host "Get-Help `"./AirRegPUCmd.ps1`" -Examples " -NoNewline -ForegroundColor White
  157. 		Write-Host "for some examples of"
  158. 		Write-Host "        `"nesting`" this script in other PowerShell or batch scripts."
  159. 		Write-Host "        Return code (`"ErrorLevel`") 1 in case of errors, otherwise 0."
  160. 		Write-Host
  161. 		Write-Host "Written by Rob van der Woude"
  162. 		Write-Host "https://www.robvanderwoude.com"
  163. 	}
  164. 	exit 1
  165. }
  166.  
  167. if ( $Help -or $Registration -match "(^\s*$|^-|\?|/)" ) {
  168. 	ShowHelp
  169. 	exit 1
  170. }
  171.  
  172. $dburl = "https://sistemas.anac.gov.br/aeronaves/cons_rab_resposta_en.asp?textMarca=$Registration"
  173. $pattern = ( 'Registration Marks\s+({0})\s*</h2>.*Manufacturer:\s*</th>\s*<td>\s*([^\n<]+?)\s*</td>\s*</tr>\s*<tr.*?<tr>\s*<th[^<]*>\s*Model:\s*</th>\s*<td>\s*([^\n<]+?)\s*</td>' -f $Registration )
  174.  
  175. if ( $Debug ) {
  176. 	$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
  177. 	Write-Host ( "Starting online search for {0} at {1}" -f $Registration, ( Get-Date ) )
  178. }
  179. $ProgressPreference = 'SilentlyContinue'
  180. $html = ( Invoke-WebRequest -Uri $dburl -Method 'Get' ).Content
  181. $ProgressPreference = 'Continue'
  182.  
  183. $html = ( $html -replace "\n"," " ) -replace "\r"," "
  184. $regex = New-Object System.Text.RegularExpressions.Regex ( $pattern, [System.Text.RegularExpressions.RegexOptions]::MultiLine )
  185. $Matches = $regex.Matches( $html )
  186.  
  187. if ( $Debug ) {
  188. 	$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
  189. 	Write-Host ( "Ended online search for {0} at {1} with {2} matches found (elapsed time {3})" -f $Registration, ( Get-Date ), $Matches.Count, $StopWatch.Elapsed )
  190. 	$StopWatch.Stop( )
  191. }
  192. if ( $Matches.Count -eq 0 ) {
  193. 	if ( !$Quiet ) {
  194. 		Write-Host "Aircraft registration for $Registration not found"
  195. 	}
  196. 	exit 1
  197. } else {
  198. 	$Manufacturer = $Matches[0].Groups[2]
  199. 	$Model = $Matches[0].Groups[3]
  200. 	"{0}`t{1}`t{2}" -f $Registration, $Manufacturer, $Model
  201. 	exit 0
  202. }
  203.  

page last modified: 2024-02-26; loaded in 0.0341 seconds