(view source code of airregncmd.ps as plain text)
<#
.SYNOPSIS
Search a downloaded FAA aircraft registration database for an aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
.DESCRIPTION
First, create a subdirectory 'N' in this script's parent folder.
Next, download the FAA Aircraft Registry's Releasable Aircraft Database (see links section), unzip it and move the files MASTER.txt and ACFTREF.txt (and optionally the other files as well) to the 'N' folder.
Now run this script with an aircraft registration as its only parameter (see examples section).
The script will first look up the MfgrModelCode for the specified registration code in the MASTER.txt file.
With the MfgrModelCode the script will look up the manufacturer and aircraf model in the ACFTREG.txt file.
If a match is found, the script will display a tab-delimited string with the registration, the manufacturer and the aircraft model (<registration><tab><manufacturer><tab><model>).
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 './AirRegNCmd.ps1' -Examples will show 2 examples of this script being called by another script.
The script contains a "second" script in a comment block, showing the "official' way to deal with the database's CSV files; however, the method used, regular expressions on plain text files, is about 4 times faster.
.PARAMETER Registration
A valid FAA aircraft registration, i.e. Nxxxxx (where x is a single alphanumeric character/digit)
.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 Debug
Show some progress messages
.OUTPUTS
A tab-delimited string <registration><tab><manufacturer><tab><model> and manufacturer and model are also stored in output variables $Manufacturer and $Model.
.EXAMPLE
. ./AirRegNCmd.ps1 "N1944S"
Will return tab-delimited string "N1944S<tab>BOEING<tab>E75", and set variables $Manufacturer to "BOEING" and $Model to "E75"
.EXAMPLE
"N1944S" | . ./AirRegNCmd.ps1
Will also return tab-delimited string "N1944S<tab>BOEING<tab>E75", and set variables $Manufacturer to "BOEING" and $Model to "E75"
.EXAMPLE
. ./AirRegNCmd.ps1 "N9ZX" -Debug
This will return:
Start searching 9ZX in MASTER file at <date> <time>
Start searching 05655US in ACFTREF file at <date> <time>
N9ZX<tab>POBEREZNY PAUL H<tab>HIPERLIGHT SNS-8
Finished at <date> <time> (elapsed time <time elapsed>)
.EXAMPLE
Create and run the following PowerShell script:
===============================================================
$Registration = 'N1944S' ; $Manufacturer = '' ; $Model = ''
[void] ( . "$PSScriptRoot\AirRegNCmd.ps1" -Registration $Registration )
Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel : {2}" -f $Registration, $Manufacturer, $Model )
===============================================================
Besides setting variables $Manufacturer to "BOEING" and $Model to "E75", it will return:
Registration : N1944S
Manufacturer : BOEING
Model : E75
.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 . ./AirRegNCmd.ps1 N1944S') DO (
ECHO Registration : %%A
ECHO Manufacturer : %%B
ECHO Model : %%C
)
===============================================================
It will return:
Registration : N1944S
Manufacturer : BOEING
Model : E75
.LINK
Script written by Rob van der Woude:
https://www.robvanderwoude.com/
.LINK
FAA Aircraft Registry's Releasable Aircraft Database:
https://www.faa.gov/licenses_certificates/aircraft_certification/aircraft_registry/releasable_aircraft_download/
.LINK
Capture -Debug parameter by mklement0 on StackOverflow.com:
https://stackoverflow.com/a/48643616
#>
param (
[parameter( ValueFromPipeline )]
[ValidatePattern("(^\s*$|[\?/-]|^N[CLPRSX]?[0-9][0-9A-Z]{2,4}$)")]
[string]$Registration,
[switch]$Quiet,
[switch]$Help
)
$progver = "1.00"
$Registration = $Registration.ToUpper( )
[string]$Manufacturer = ''
[string]$Model = ''
[bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
function ShowHelp( $errormessage = '' ) {
if ( !$Quiet ) {
if ( $errormessage ) {
Write-Host
Write-Host "Error: " -ForegroundColor Red -NoNewline
Write-Host $errormessage
}
Write-Host
Write-Host ( "AirRegNCmd.ps1, Version {0}" -f $progver )
Write-Host "Search downloaded FAA aircraft registration database for a registation"
Write-Host
Write-Host "Usage: " -NoNewline
Write-Host ". ./AirRegNCmd.ps1 [-Registration] N**** [-Quiet] [-Debug] [-Help]" -ForegroundColor White
Write-Host
Write-Host "Where: " -NoNewline
Write-Host "N**** " -NoNewline -ForegroundColor White
Write-Host "is a valid FAA aircraft registration, e.g. N1944S"
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 requires a downloaded FAA aircraft registration database,"
Write-Host " located in a subfolder 'N' of this script's parent folder."
Write-Host " The FAA aircraft registration database can be downloaded at:"
Write-Host " https://www.faa.gov/licenses_certificates/aircraft_certification" -ForegroundColor DarkGray
Write-Host " /aircraft_registry/releasable_aircraft_download/" -ForegroundColor DarkGray
Write-Host " The result, if any, of the search is displayed as tab-delimited text:"
Write-Host " <registration><tab><manufacturer><tab><model>"
Write-Host " Besides its screen output, this script will also set the `$Manufacturer"
Write-Host " and `$Model variables with the database search result."
Write-Host " Run " -NoNewline
Write-Host "Get-Help `"./AirRegNCmd.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 "Credits: Code to capture -Debug parameter by mklement0 on StackOverflow.com:"
Write-Host " https://stackoverflow.com/a/48643616" -ForegroundColor DarkGray
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
}
$N_number = $Registration -replace "^N[CLPRSX]?",""
$dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'N' )
$masterfile = ( Join-Path -Path $dbfolder -ChildPath 'MASTER.txt' )
$acftreffile = ( Join-Path -Path $dbfolder -ChildPath 'ACFTREF.txt' )
if ( ( Test-Path -Path $masterfile -PathType 'Leaf' ) -and ( Test-Path -Path $acftreffile -PathType 'Leaf' ) ) {
<#
# Method 1: treat files as CSV
# Time elapsed during test runs: approximately 140 seconds
#
if ( $Debug ) {
$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
Write-Host ( "Start reading MASTER table at {0}" -f ( Get-Date ) )
}
$found = $false # will be set to True when a matching aircraft is found in the database
$mastertable = Import-Csv -Path $masterfile -Delimiter ','
if ( $Debug ) {
Write-Host ( "Start reading ACFTREF table at {0}" -f ( Get-Date ) )
}
$acftreftable = Import-Csv -Path $acftreffile -Delimiter ','
if ( $Debug ) {
Write-Host ( "Start searching {0} in MASTER table at {1}" -f $N_number, ( Get-Date ) )
}
foreach ( $record in $mastertable ) {
if ( !$found ) {
if ( $record.'N-NUMBER'.Trim( ) -eq $N_number ) {
$ManufacturerModelCode = $record.'MFR MDL CODE'.Trim( )
if ( $Debug ) {
Write-Host ( "Start searching {0} in ACFTREF table at {1}" -f $ManufacturerModelCode, ( Get-Date ) )
}
foreach ( $item in $acftreftable ) {
if ( !$found ) {
if ( $item.'CODE' -eq $ManufacturerModelCode ) {
$Manufacturer = $item.'MFR'.Trim( )
$Model = $item.'MODEL'.Trim( )
"{0}`t{1}`t{2}" -f $Registration.ToUpper( ), $Manufacturer, $Model | Out-String
$found = $true
}
}
}
}
}
}
if ( $Debug ) {
Write-Host ( "Finished at {0} (elapsed time {1})`n`n" -f ( Get-Date ), $StopWatch.Elapsed )
$StopWatch.Stop( )
}
#>
# Method 2: treat files as plain text and use regular expressions to find matches
# Time elapsed during test runs: approximately 35 seconds, about 4 times as fast as method 1
#
if ( $Debug ) {
$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
Write-Host ( "Start searching {0} in MASTER file at {1}" -f $N_number, ( Get-Date ) )
}
$pattern = "^{0}\s*,[^\n\r]+" -f $N_number
$record = ( ( Get-Content -Path $masterfile ) -match $pattern )
if ( $record ) {
$ManufacturerModelCode = $record.Split( ',' )[2].Trim( )
if ( $Debug ) {
Write-Host ( "Start searching {0} in ACFTREF file at {1}" -f $ManufacturerModelCode, ( Get-Date ) )
}
$pattern = "^{0}\s*,[^\n\r]+" -f $ManufacturerModelCode
$record = ( ( Get-Content -Path $acftreffile ) -match $pattern )
if ( $record ) {
$Manufacturer = $record.Split( ',' )[1].Trim( )
$Model = $record.Split( ',' )[2].Trim( )
}
}
"{0}`t{1}`t{2}" -f $Registration.ToUpper( ), $Manufacturer, $Model | Out-String
if ( $Debug ) {
Write-Host ( "Finished at {0} (elapsed time {1})`n`n" -f ( Get-Date ), $StopWatch.Elapsed )
$StopWatch.Stop( )
}
} else {
if ( $Quiet ) {
if ( $Debug ) {
Write-Host "Downloaded FAA Aircraft Registry's Releasable Aircraft Database not found"
}
exit 1
} else {
$message = "No downloaded FAA Aircraft Registry's Releasable Aircraft Database was found.`n`nDo you want to open the download webpage for the database now?"
$title = "No Database Found"
$buttons = "YesNo"
Add-Type -AssemblyName 'System.Windows.Forms'
$answer = [System.Windows.Forms.MessageBox]::Show( $message, $title, $buttons )
if ( $answer -eq 'Yes' ) {
$url = 'https://www.faa.gov/licenses_certificates/aircraft_certification/aircraft_registry/releasable_aircraft_download/'
Start-Process $url
} else {
ShowHelp( "No downloaded FAA Aircraft Registry's Releasable Aircraft Database found, please download it and try again" )
}
}
}
page last modified: 2024-04-16; loaded in 0.0117 seconds