(view source code of airregvhcmd.ps as plain text)
<#
.SYNOPSIS
Search a downloaded Australian aircraft registry database for an Australian aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
.DESCRIPTION
First, create a subdirectory 'VH' in this script's parent folder.
Next, download the Australian aircraft registry database (see links section), unZIP and move the file acrftreg.csv to the 'VH' folder.
Now run this script with an Australian aircraft registration as its only parameter (see examples section).
The script will first look for the specified registration code in column 1 of the acrftreg.csv file.
If a match is found, the manufacturer and model are found in columns 2 and 4.
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 './AirRegVHCmd.ps1' -Examples will show 2 examples of this script being called by another script.
.PARAMETER Registration
A valid Australian aircraft registration, e.g. VH-EYV
.PARAMETER Quiet
Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1.
.PARAMETER Version
Show this script's version number; if combined with -Verbose show full script path, version number and last/modified/release date
.PARAMETER CheckDB
If the required local database can be found, returns True and exit code 0; if not, returns False and exit code 1.
.PARAMETER Help
Show the script's help screen
.PARAMETER Debug
Show some progress messages
.OUTPUTS
If a match is found: a tab-delimited string with <registration><tab><manufacturer_and_model>, and if -NoBreak switch is used: variables $Manufacturer set to aircraft manufacturer $Model set to aircraft model
If no match is found: False
.EXAMPLE
. ./AirRegVHCmd.ps1 VH-EYV
Will return tab-delimited string "VH-EYV<tab>THE BOEING COMPANY<tab>A75N1", and set variables $Manufacturer to "THE BOEING COMPANY" and $Model to "A75N1"
.EXAMPLE
"VH-EYV" | . ./AirRegVHCmd.ps1
Will also return tab-delimited string "VH-EYV<tab>THE BOEING COMPANY<tab>A75N1", and set variables $Manufacturer to "THE BOEING COMPANY" and $Model to "A75N1"
.EXAMPLE
. ./AirRegVHCmd.ps1 "VH-EYV" -Debug
This will return:
Start searching "EYV" in acrftreg.csv file at <date> <time>
Found a match at <date> <time>
VH-EYV<tab>THE BOEING COMPANY<tab>A75N1
Finished at <date> <time> (elapsed time <time elapsed>)
.EXAMPLE
Create and run the following PowerShell script:
===============================================================
$Registration = 'VH-EYV' ; $Manufacturer = '' ; $Model = ''
[void] ( . "$PSScriptRoot\AirRegVHCmd.ps1" -Registration $Registration )
Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel : {2}" -f $Registration, $Manufacturer, $Model )
===============================================================
Besides setting variables $Manufacturer to "THE BOEING COMPANY" and $Model to "A75N1", it will return:
Registration : VH-EYV
Manufacturer : THE BOEING COMPANY
Model : A75N1
.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 . ./AirRegVHCmd.ps1 VH-EYV') DO (
ECHO Registration : %%A
ECHO Manufacturer : %%B
ECHO Model : %%C
)
===============================================================
It will return:
Registration : VH-EYV
Manufacturer : THE BOEING COMPANY
Model : A75N1
.LINK
Script written by Rob van der Woude:
https://www.robvanderwoude.com/
.LINK
Australian Civil Aviation Safety Authority aircraft registry database:
https://www.casa.gov.au/aircraft/civil-aircraft-register/aircraft-register-data-files
.LINK
Capture -Debug parameter by mklement0 on StackOverflow.com:
https://stackoverflow.com/a/48643616
#>
param (
[parameter( ValueFromPipeline )]
[ValidatePattern("(^\s*$|[\?/]|^-|^VH-[A-Z]{3}$)")]
[string]$Registration,
[switch]$CheckDB,
[switch]$Version,
[switch]$Quiet,
[switch]$Help
)
$progver = "1.00"
$Registration = $Registration.ToUpper( )
$Manufacturer = ''
$Model = ''
[bool]$Debug = ( $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
}
$dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'VH' )
$dbfile = ( Join-Path -Path $dbfolder -ChildPath 'acrftreg.csv' )
if ( $CheckDB ) {
if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
[bool]$true
exit 0
} else {
[bool]$false
exit 1
}
}
if ( $Help -or [string]::IsNullOrWhiteSpace( $Registration ) -or ( $Registration -match "[/\?]" ) ) {
Clear-Host
Write-Host ( "`"{0}`", Version {1}" -f $PSCommandPath, $progver ) -NoNewline
$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
Write-Host ", release date " -NoNewline
} else {
# if last modified time is not equal to program version, the script has been tampered with
Write-Host ", last modified date " -NoNewline
}
Write-Host $lastmod.ToString( "yyyy-MM-dd" )
Write-Host
Get-Help $PSCommandPath -Full
exit -1
}
$VH_num = $Registration.Substring( 3 )
if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
if ( $Debug ) {
$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
Write-Host ( "Start searching `"{0}`" in acrftreg.csv file at {1}" -f $VH_num, ( Get-Date ) )
}
$pattern = "^`"{0}`",`"[^\n\r]+" -f $VH_num
$record = ( ( Get-Content -Path $dbfile ) -match $pattern )
if ( $record ) {
if ( $record.Split( ',' ).Count -gt 3 ) {
$Manufacturer = $record.Split( ',' )[1] -replace "`"",""
$Model = $record.Split( ',' )[3] -replace "`"",""
if ( $Debug ) {
Write-Host ( "Found a match at {0}" -f ( Get-Date ) )
}
}
}
"{0}`t{1}`t{2}" -f $Registration, $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 Australian aircraft registry database file `"{0}`" not found" -f $dbfile )
}
exit 1
} else {
$message = "No downloaded Australian aircraft registry 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.casa.gov.au/aircraft/civil-aircraft-register/aircraft-register-data-files'
Start-Process $url
} else {
ShowHelp( 'No downloaded Australian aircraft registry database found, please download it and try again' )
}
}
}
page last modified: 2024-04-16; loaded in 0.0088 seconds