(view source code of airregopenskycmd.ps as plain text)
<#
.SYNOPSIS
Search the downloaded OpenSky Network aircraft registration database for an aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
.DESCRIPTION
First create a subdirectory 'OpenSkyNetwork' in this script's parent directory.
Next download the OpenSky Network aircraft registration database (see links section), we will be using aircraft-database-complete-{year}-{month}.csv only for this script.
Run this script with an aircraft registration as its only parameter (see examples section).
The script will try to find the registration in the downloaded aircraft registry.
If a match is found, the script will display a tab-delimited string with the registration and the manufacturer/model (<registration><tab><tab><manufacturer_and_model>).
If the script was started by another PowerShell script with the -NoBreak switch, the calling PowerShell script may also read the manufacturer/model from the variable $Manufacturer, 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/model.
Get-Help './AirRegOpenSkyCmd.ps1' -Examples will show 2 examples of this script being called by another script.
.PARAMETER Registration
A valid aircraft registration, e.g. N1944S, OK-NUL or OO-CAT
.PARAMETER Quiet
Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1
.PARAMETER NoBreak
Do not break the search loop after a match is found.
On average searches take about twice as long when using this switch; hence it is recommended to use it only if:
-*- this script is called by another PowerShell script
and:
-*- the calling PowerShell script uses the variables $Manufacturer and $Model set by this script
.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; implies -NoBreak
.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
. ./AirRegOpenSkyCmd.ps1 "OO-CAT"
Will return tab-delimited string "OO-CAT<tab>Amateur Built<tab>Jodel D.112"
.EXAMPLE
"OO-CAT" | . ./AirRegOpenSkyCmd.ps1 -NoBreak
Will return tab-delimited string "OO-CAT<tab>Amateur Built<tab>Jodel D.112" and set variables $Manufacturer to "Amateur Built" and $Model to "Jodel D.112"
.EXAMPLE
. ./AirRegOpenSkyCmd.ps1 -Version -Verbose
Will return the full script path, version and last modified/release date
.EXAMPLE
. ./AirRegOpenSkyCmd.ps1 -Version
Will return the script version
.EXAMPLE
. ./AirRegOpenSkyCmd.ps1 OO-CAT -Debug
Will return:
Started searching for OO-CAT in ".\OpenSkyNetwork\aircraftDatabase.csv" at <date> <time>
Found a match at <date> <time>
OO-CAT Amateur Built Jodel D.112
Finished at <date> <time> (time elapsed <elapsed time>)
.EXAMPLE
Create and run the following PowerShell script:
===============================================================
$Registration = 'OO-CAT' ; $Model = ''
[void] ( . "$PSScriptRoot\AirRegOpenSkyCmd.ps1" -Registration $Registration -NoBreak )
Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel : {2}" -f $Registration, $Manufacturer, $Model )
===============================================================
Besides setting variables $Manufacturer to "Amateur Built" and $Model to "Jodel D.112", it will return:
Registration : OO-CAT
Manufacturer : Amateur Built
Model : Jodel D.112
.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 . ./AirRegOpenSkyCmd.ps1 OO-CAT') DO (
ECHO Registration : %%A
ECHO Manufacturer : %%B
ECHO Model : %%C
)
===============================================================
It will return:
Registration : OO-CAT
Manufacturer : Amateur Built
Model : Jodel D.112
.LINK
Script written by Rob van der Woude:
https://www.robvanderwoude.com/
.LINK
OpenSky Network aircraft registration database:
https://opensky-network.org/datasets/metadata/
.LINK
Capture -Debug and -Verbose parameter by mklement0 on StackOverflow.com:
https://stackoverflow.com/a/48643616
#>
param (
[parameter( ValueFromPipeline )]
[ValidatePattern("(^\s*$|[\?/]|^\w[\w-]{2,3}\w+)")]
[string]$Registration,
[switch]$Quiet,
[switch]$NoBreak,
[switch]$Help,
[switch]$Version
)
$progver = "1.02"
$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
}
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
}
$found = $false
$dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'OpenSkyNetwork' )
if ( Test-Path -Path $dbfolder -PathType 'Container' ) {
$dbfile = $( Get-ChildItem -Path $dbfolder -Name aircraft-database-complete-*.csv | Sort-Object | Select-Object -Last 1 )
$dbfile = ( Join-Path -Path $dbfolder -ChildPath $dbfile )
if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
if ( $Debug ) {
$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
Write-Host ( "Started searching for {0} in `"{1}`" at {2}" -f $Registration, $dbfile, ( Get-Date ) )
}
( Get-Content $dbfile ).Split( "`n" ) | Select-String -Pattern "`"$Registration`"" -AllMatches | ForEach-Object {
if ( !$found ) {
$fields = $_.ToString( ).Split( ',' )
if ( $fields[1] -match "`"$Registration`"" ) {
if ( $Debug ) {
Write-Host ( "Found a match at {0}" -f ( Get-Date ) )
}
$Manufacturer = $fields[3]
$Model = $fields[4]
( "{0}`t{1}`t{2}" -f $fields[1], $Manufacturer, $Model ) -replace '"',''
if ( $Debug ) {
Write-Host ( ( "{0}`t{1}`t{2}" -f $fields[1], $Manufacturer, $Model ) -replace '"','' )
}
$found = $true
if ( -not $NoBreak ) {
if ( $Debug ) {
Write-Host ( "Finished at {0} (time elapsed {1})" -f ( Get-Date ), $StopWatch.Elapsed )
$StopWatch.Stop( )
} else {
exit 0
}
}
}
}
}
if ( $Debug ) {
Write-Host ( "Finished at {0} (time elapsed {1})" -f ( Get-Date ), $StopWatch.Elapsed )
$StopWatch.Stop( )
}
} else {
if ( -not $Quiet ) {
Write-Host ( "Database file `"{0}`" not found" -f $dbfile )
}
}
} else {
if ( -not $Quiet ) {
Write-Host ( "Folder `"{0}`" not found" -f $dbfolder )
}
}
if ( !$found ) {
$false
}
page last modified: 2024-04-16; loaded in 0.0095 seconds