(view source code of airregzkcmd.ps as plain text)
<#
.SYNOPSIS
Search a downloaded New Zealand aircraft registry database for an New Zealand 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 New Zealand aircraft registry database (see links section), unZIP and move the file AircraftRegisterExport.tab to the 'ZK' folder.
Now run this script with an New Zealand aircraft registration as its only parameter (see examples section).
The script will first look for the word 'Aeroplane' in column 1 and the specified registration code in column 2 of the tab-delimited AircraftRegisterExport.tab file.
If a match is found, the manufacturer and model are found in columns 3 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 './AirRegZKCmd.ps1' -Examples will show 2 examples of this script being called by another script.
.PARAMETER Registration
A valid NewZealand aircraft registration, e.g. ZK-ADI
.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
. ./AirRegZKCmd.ps1 ZK-ADI
Will return tab-delimited string "ZK-ADI<tab>De Havilland<tab>DH 83 Fox Moth", and set variables $Manufacturer to "De Havilland" and $Model to "DH 83 Fox Moth"
.EXAMPLE
"ZK-ADI" | . ./AirRegZKCmd.ps1
Will also return tab-delimited string "ZK-ADI<tab>De Havilland<tab>DH 83 Fox Moth", and set variables $Manufacturer to "De Havilland" and $Model to "DH 83 Fox Moth"
.EXAMPLE
. ./AirRegZKCmd.ps1 "ZK-ADI" -Debug
This will return:
Start searching "ADI" in AircraftRegisterExport.tab at <date> <time>
Found a match at <date> <time>
ZK-ADI De Havilland DH 83 Fox Moth
Finished at <date> <time> (elapsed time <time elapsed>)
.EXAMPLE
Create and run the following PowerShell script:
===============================================================
$Registration = ZK-ADI' ; $Manufacturer = '' ; $Model = ''
[void] ( . "$PSScriptRoot\AirRegZKCmd.ps1" -Registration $Registration )
Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel : {2}" -f $Registration, $Manufacturer, $Model )
===============================================================
Besides setting variables $Manufacturer to "De Havilland" and $Model to "DH 83 Fox Moth", it will return:
Registration : ZK-ADI
Manufacturer : De Havilland
Model : DH 83 Fox Moth
.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 . ./AirRegZKCmd.ps1 ZK-ADI') DO (
ECHO Registration : %%A
ECHO Manufacturer : %%B
ECHO Model : %%C
)
===============================================================
It will return:
Registration : ZK-ADI
Manufacturer : De Havilland
Model : DH 83 Fox Moth
.LINK
Script written by Rob van der Woude:
https://www.robvanderwoude.com/
.LINK
New Zealand Civil Aviation Authority, Aviation Security Service's aircraft registry database:
https://www.aviation.govt.nz/aircraft/aircraft-registration/aircraft-register-search/
.LINK
Capture -Debug parameter by mklement0 on StackOverflow.com:
https://stackoverflow.com/a/48643616
#>
param (
[parameter( ValueFromPipeline )]
[ValidatePattern("(^\s*$|[\?/]|^-|^ZK-[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 'ZK' )
$dbfile = ( Join-Path -Path $dbfolder -ChildPath 'AircraftRegisterExport.tab' )
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
}
$ZK_num = $Registration.Substring( 3 )
if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
if ( $Debug ) {
$StopWatch = [system.diagnostics.stopwatch]::StartNew( )
Write-Host ( "Start searching `"{0}`" in AircraftRegisterExport.tab at {1}" -f $ZK_num, ( Get-Date ) )
}
$pattern = "^Aeroplane`t{0}`t[^\n\r]+" -f $ZK_num
$record = ( ( Get-Content -Path $dbfile ) -match $pattern )
if ( $record ) {
if ( $record.Split( "`t" ).Count -gt 3 ) {
$Manufacturer = $record.Split( "`t" )[2]
$Model = $record.Split( "`t" )[3]
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 New Zealand aircraft registry database file `"{0}`" not found" -f $dbfile )
}
exit 1
} else {
$message = "No downloaded New Zealand 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.aviation.govt.nz/aircraft/aircraft-registration/aircraft-register-search/'
Start-Process $url
} else {
ShowHelp( 'No downloaded New Zealand aircraft registry database found, please download it and try again' )
}
}
}
page last modified: 2024-04-16; loaded in 0.0112 seconds