<# .SYNOPSIS 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) .DESCRIPTION Run this script with an aircraft registration as its only parameter (see examples section). The script will try to find the registration in the online Brasilian Agência Nacional De Ação Civil aircraft registry. If a match is found, the script will display a tab-delimited string with the registration, manufacturer and model (). 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. 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. 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. Get-Help './AirRegPSCmd.ps1' -Examples will show 2 examples of this script being called by another script. .PARAMETER Registration A valid Brasilian aircraft registration, i.e. PPxxx or PRxxx .. PUxxx (where each x is a single letter, no dash) .PARAMETER Quiet Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1 .PARAMETER Help Show the script's help screen .PARAMETER Version Show this script's version number; if combined with -Verbose show full script path, version number and last/modified/release date .PARAMETER Debug Show some progress messages .OUTPUTS A tab-delimited string with , and variable $Manufacturer and $Model set to manufacturer and model .EXAMPLE . ./AirRegPSCmd.ps1 "PRATX" Will return tab-delimited string "PRATXCESSNA AIRCRAFTA188B" and set variables $Manufacturer to "CESSNA AIRCRAFT" and $Model to "A188B" .EXAMPLE "PRATX" | . ./AirRegPSCmd.ps1 Will also return tab-delimited string "PRATXCESSNA AIRCRAFTA188B" and set variables $Manufacturer to "CESSNA AIRCRAFT" and $Model to "A188B" .EXAMPLE . ./AirRegPSCmd.ps1 -Version -Verbose Will return the full script path, version and last modified/release date .EXAMPLE . ./AirRegPSCmd.ps1 -Version Will return the script version .EXAMPLE Create and run the following PowerShell script: =============================================================== $Registration = 'PRATX' ; $Model = '' [void] ( . "$PSScriptRoot\AirRegPSCmd.ps1" -Registration $Registration ) Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel : {2}" -f $Registration, $Manufacturer, $Model ) =============================================================== Besides setting variables $Manufacturer to "CESSNA AIRCRAFT" and $Model to "A188B", it will return: Registration : PRATX Manufacturer : CESSNA AIRCRAFT Model : A188B .EXAMPLE Create and run the following batch file: =============================================================== REM Note that there should only be a TAB and nothing else between delims= and the doublequote FOR /F "tokens=1-3 delims= " %%A IN ('powershell . ./AirRegPSCmd.ps1 PRATX') DO ( ECHO Registration : %%A ECHO Manufacturer : %%B ECHO Model : %%C ) =============================================================== It will return: Registration : PRATX Manufacturer : CESSNA AIRCRAFT Model : A188B .LINK Script written by Rob van der Woude: https://www.robvanderwoude.com/ .LINK Brasilian Agência Nacional De Ação Civil online aircraft registration database: https://sistemas.anac.gov.br/aeronaves/cons_rab_en.asp .LINK Capture -Debug and -Verbose parameter by mklement0 on StackOverflow.com: https://stackoverflow.com/a/48643616 #> param ( [parameter( ValueFromPipeline )] [ValidatePattern("(^\s*$|[\?/]|^P[PRSTU][A-Z]{3}$)")] [string]$Registration, [switch]$Quiet, [switch]$Help, [switch]$Version ) $progver = "1.01" $Registration = $Registration.ToUpper( ) $Manufacturer = '' $Model = '' [bool]$Debug = $Debug -or ( $PSBoundParameters.ContainsKey( 'Debug' ) ) [bool]$Verbose = ( $PSBoundParameters.ContainsKey( 'Verbose' ) ) if ( $Version ) { if ( $Verbose ) { $lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) ) if ( $lastmod.ToString( "h.mm" ) -eq $progver ) { "`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" ) } else { # if last modified time is not equal to program version, the script has been tampered with "`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" ) } } else { $progver } exit 0 } function ShowHelp( $message = '' ) { if ( !$Quiet ) { if ( $errormessage ) { Write-Host Write-Host "Error: " -ForegroundColor Red -NoNewline Write-Host $errormessage } Write-Host Write-Host ( "AirRegPSCmd.ps1, Version {0}" -f $progver ) Write-Host "Search online Brasilian aircraft registration database for a registation" Write-Host Write-Host "Usage: " -NoNewline Write-Host ". ./AirRegPSCmd.ps1 [-Registration] PS*** [-Quiet] [-Debug] [-Help]" -ForegroundColor White Write-Host Write-Host "Where: " -NoNewline Write-Host "PS*** " -NoNewline -ForegroundColor White Write-Host "is a valid Brasilian aircraft registration, e.g. PSAAZ" Write-Host " -Quiet " -NoNewline -ForegroundColor White Write-Host "all errors are ignored and no error messages displayed" Write-Host " -Debug " -NoNewline -ForegroundColor White Write-Host "shows some progress messages" Write-Host " -Help " -NoNewline -ForegroundColor White Write-Host "shows this help screen" Write-Host Write-Host "Notes: This script uses the Brasilian Agência Nacional De Ação Civil aircraft" Write-Host " registration database at:" Write-Host " https://sistemas.anac.gov.br/aeronaves/cons_rab_en.asp" -ForegroundColor DarkGray Write-Host " The result, if any, of the search is displayed as tab-delimited text:" Write-Host " " Write-Host " Besides its screen output, this script will also set the `SManufacturer" Write-Host " and `$Model variables with the database search result." Write-Host " Run " -NoNewline Write-Host "Get-Help `"./AirRegPSCmd.ps1`" -Examples " -NoNewline -ForegroundColor White Write-Host "for some examples of" Write-Host " `"nesting`" this script in other PowerShell or batch scripts." Write-Host " Return code (`"ErrorLevel`") 1 in case of errors, otherwise 0." Write-Host Write-Host "Written by Rob van der Woude" Write-Host "https://www.robvanderwoude.com" } exit 1 } if ( $Help -or $Registration -match "(^\s*$|^-|\?|/)" ) { ShowHelp exit 1 } $dburl = "https://sistemas.anac.gov.br/aeronaves/cons_rab_resposta_en.asp?textMarca=$Registration" $pattern = ( 'Registration Marks\s+({0})\s*.*Manufacturer:\s*\s*\s*([^\n<]+?)\s*\s*\s*\s*\s*Model:\s*\s*\s*([^\n<]+?)\s*' -f $Registration ) if ( $Debug ) { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew( ) Write-Host ( "Starting online search for {0} at {1}" -f $Registration, ( Get-Date ) ) } $ProgressPreference = 'SilentlyContinue' $html = ( Invoke-WebRequest -Uri $dburl -Method 'Get' ).Content $ProgressPreference = 'Continue' $html = ( $html -replace "\n"," " ) -replace "\r"," " $regex = New-Object System.Text.RegularExpressions.Regex ( $pattern, [System.Text.RegularExpressions.RegexOptions]::MultiLine ) $Matches = $regex.Matches( $html ) if ( $Debug ) { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew( ) Write-Host ( "Ended online search for {0} at {1} with {2} matches found (elapsed time {3})" -f $Registration, ( Get-Date ), $Matches.Count, $StopWatch.Elapsed ) $StopWatch.Stop( ) } if ( $Matches.Count -eq 0 ) { if ( !$Quiet ) { Write-Host "Aircraft registration for $Registration not found" } exit 1 } else { $Manufacturer = $Matches[0].Groups[2] $Model = $Matches[0].Groups[3] "{0}`t{1}`t{2}" -f $Registration, $Manufacturer, $Model exit 0 }