(view source code of airregccmd.ps as plain text)
<#
.SYNOPSIS
Search a downloaded Canadian Civil Aircraft Register database for an aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
.DESCRIPTION
First, create a subdirectory 'C' in this script's parent folder.
Next, download the Canadian Civil Aircraft Register database (see links section), unzip it and move the file carscurr.txt (and optionally the other files as well) to the 'C' folder.
Now run this script with an aircraft registration as its only parameter (see examples section).
The script will first look for the specified registration code in column 1 of the carscurr.txt file.
If a match is found, the manufacturer and model are found in columns 4 and 5.
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 './AirRegCCmd.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 Canadian aircraft registration, i.e. C-xxx or C-xxxx (where each 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
. ./AirRegCCmd.ps1 "C-AHA"
Will return tab-delimited string "N1944S<tab>BOEING<tab>E75N1", and set variables $Manufacturer to "BOEING" and $Model to "E75N1"
.EXAMPLE
"C-AHA" | . ./AirRegCCmd.ps1
Will also return tab-delimited string "N1944S<tab>BOEING<tab>E75N1", and set variables $Manufacturer to "BOEING" and $Model to "E75N1"
.EXAMPLE
. ./AirRegCCmd.ps1 "C-AHA" -Debug
This will return:
Start searching AHA in carscurr.txt file at <date> <time>
C-AHA Boeing E75N1
Finished at <date> <time> (elapsed time <time elapsed>)
.EXAMPLE
Create and run the following PowerShell script:
===============================================================
$Registration = 'C-AHA' ; $Manufacturer = '' ; $Model = ''
[void] ( . "$PSScriptRoot\AirRegCCmd.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 : C-AHA
Manufacturer : BOEING
Model : E75N1
.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 . ./AirRegCCmd.ps1 C-AHA') DO (
ECHO Registration : %%A
ECHO Manufacturer : %%B
ECHO Model : %%C
)
===============================================================
It will return:
Registration : C-AHA
Manufacturer : BOEING
Model : E75N1
.LINK
Script written by Rob van der Woude:
https://www.robvanderwoude.com/
.LINK
Canadian Civil Aircraft Register database:
https://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx
.LINK
Capture -Debug parameter by mklement0 on StackOverflow.com:
https://stackoverflow.com/a/48643616
#>
param (
[parameter( ValueFromPipeline )]
[ValidatePattern("(^\s*$|[\?/-]|^C-[A-Z]{3,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 ( "AirRegCCmd.ps1, Version {0}" -f $progver )
Write-Host "Search downloaded Canadian Civil Aircraft Register database for a registation"
Write-Host
Write-Host "Usage: " -NoNewline
Write-Host ". ./AirRegCCmd.ps1 [-Registration] C-*** [-Quiet] [-Debug] [-Help]" -ForegroundColor White
Write-Host
Write-Host "Where: " -NoNewline
Write-Host "C-*** " -NoNewline -ForegroundColor White
Write-Host "is a valid Canadian aircraft registration, e.g. C-AHA"
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 Canadian Civil Aircraft Register"
Write-Host " database, located in a subfolder 'C' of this script's parent folder."
Write-Host " The Canadian Civil Aircraft Register database can be downloaded at:"
Write-Host " https://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx" -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 `"./AirRegCCmd.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
}
$C_number = $Registration.Substring( 2 )
$dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'C' )
$dbfile = ( Join-Path -Path $dbfolder -ChildPath 'carscurr.txt' )
if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
if ( $Debug ) {
$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
Write-Host ( "Start searching {0} in carscurr.txt file at {1}" -f $C_number, ( Get-Date ) )
}
$pattern = '^\"{0,4}\",[^\n\r]+' -f $C_number
$record = ( ( Get-Content -Path $dbfile ) -match $pattern )
if ( $record ) {
$Manufacturer = ( $record.Split( ',' )[3] -replace '(^\"|\s*\")','' )
$Model = ( $record.Split( "," )[4] -replace '(^\"|\s*\")','' )
}
"{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 Canadian Civil Aircraft Register database file `"{0}`" not found" -f $dbfile )
}
exit 1
} else {
$message = "No downloaded Canadian Civil Aircraft Register 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://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx'
Start-Process $url
} else {
ShowHelp( 'No downloaded Canadian Civil Aircraft Register database found, please download it and try again' )
}
}
}
page last modified: 2024-04-16; loaded in 0.0135 seconds