(view source code of airregoecmd.ps as plain text)
<#
.SYNOPSIS
Search a downloaded Austrian aircraft register database for an aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
.DESCRIPTION
First, create a subdirectory 'OE' in this script's parent folder.
Next, download the Austrian aircraft register database (see links section), unzip it and move the PDF file to the 'OE' folder.
Now run this script with an aircraft registration as its only parameter (see examples section).
The script cannot read the PDF file directly, so the PDF file needs to be converted to plain text first.
The script will look for a GhostScript installation on the computer, and if found, uses the following command to convert the PDF file to plain text:
"C:\Program Files\gs\gs<version>\bin\gswin64c.exe" -sDEVICE=txtwrite -o Gesamt_<year>_EN.txt Gesamt_<year>_EN.pdf
NOTE: If you download a new PDF file, make sure to either run this script with the -ConvertPDF switch or manually delete the old TEXT file!
After removing "OE-" from the registration, the script will search for a line that starts with the remainder of the registration.
If a match is found, the manufacturer will be in the same line, more to the right, and the model will be on the next line right below the manufacturer.
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 './AirRegOECmd.ps1' -Examples will show 2 examples of this script being called by another script.
.PARAMETER Registration
A valid Austrian aircraft registration, i.e. OE-xxx (where each x is a single character)
.PARAMETER ConvertPDF
Force conversion of the latest PDF file to plain text; use this switch once after downloading a new PDF file.
.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 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
.OUTPUTS
A tab-delimited string <registration><tab><manufacturer><tab><model> and manufacturer and model are also stored in output variables $Manufacturer and $Model.
.EXAMPLE
. ./AirRegOECmd.ps1 "OE-AKJ"
Will return tab-delimited string "OE-AKJ<tab>Boeing Commercial Airplane Group<tab>E75", and set variables $Manufacturer to "BOEING" and $Model to "E75N1"
.EXAMPLE
"OE-AKJ" | . ./AirRegOECmd.ps1
Will also return tab-delimited string "OE-AKJ<tab>Boeing Commercial Airplane Group<tab>E75", and set variables $Manufacturer to "BOEING" and $Model to "E75N1"
.EXAMPLE
. ./AirRegOECmd.ps1 "OE-AKJ" -Debug
This will return:
Start searching AHA in carscurr.txt file at <date> <time>
OE-AKJ Boeing Commercial Airplane Group E75
Finished at <date> <time> (elapsed time <time elapsed>)
.EXAMPLE
. ./AirRegOECmd.ps1 -Version -Verbose
Will return the full script path, version and last modified/release date
.EXAMPLE
. ./AirRegOECmd.ps1 -Version
Will return the script version
.EXAMPLE
Create and run the following PowerShell script:
===============================================================
$Registration = 'OE-AKJ' ; $Manufacturer = '' ; $Model = ''
[void] ( . "$PSScriptRoot\AirRegOECmd.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 : OE-AKJ
Manufacturer : Boeing Commercial Airplane Group
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 . ./AirRegOECmd.ps1 OE-AKJ') DO (
ECHO Registration : %%A
ECHO Manufacturer : %%B
ECHO Model : %%C
)
===============================================================
It will return:
Registration : OE-AKJ
Manufacturer : Boeing Commercial Airplane Group
Model : E75
.LINK
Script written by Rob van der Woude:
https://www.robvanderwoude.com/
.LINK
Austrian aircraft register database:
https://www.austrocontrol.at/en/aviation_agency/aircraft/aircraft_register/overview__supplement
.LINK
GhostScript download page:
https://www.ghostscript.com/download/gsdnld.html
.LINK
Convert PDF to text by user2176753 on StackOverflow.com:
https://stackoverflow.com/a/26405241
.LINK
Capture -Debug parameter by mklement0 on StackOverflow.com:
https://stackoverflow.com/a/48643616
#>
param (
[parameter( ValueFromPipeline )]
[ValidatePattern("(^\s*$|[\?/]|^OE-[A-Z]{3}$)")]
[string]$Registration,
[switch]$ConvertPDF,
[switch]$Quiet,
[switch]$Help,
[switch]$Version
)
$progver = "1.01"
$Registration = $Registration.ToUpper( )
[string]$Manufacturer = ''
[string]$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
}
function ShowHelp( $errormessage = '' ) {
if ( !$Quiet ) {
Clear-Host
if ( $errormessage ) {
Write-Host
Write-Host "Error: " -ForegroundColor Red -NoNewline
Write-Host $errormessage
}
Write-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
}
}
if ( $Help -or $Registration -match '(^\s*$|[\?/])' ) {
ShowHelp
exit 1
}
function GetGhostscript( ) {
# Check if GhostScript is installed, and if so, return the path to the command line execuatable
$gskey = $null
$gspath = ''
$gsprog = ''
if ( $Debug ) {
Write-Host ( "Start searching the registry for GhostScript installations at {0}" -f ( Get-Date ) )
}
$gskey = ( ( Get-ChildItem -Path registry::'HKEY_LOCAL_MACHINE\SOFTWARE' -Recurse -Depth 2 -ErrorAction SilentlyContinue | Where-Object { $_.Name -match 'ghostscript' } | Select-Object -First 1 ) | Get-ChildItem | Sort-Object { [double]$_.PSChildName } | Select-Object -Last 1 ).Name
if ( $gskey ) {
if ( $Debug ) {
Write-Host ( "Found GhostScript installation in the registry: `"{0}`" at {1}" -f $gskey, ( Get-Date ) )
}
$gspath = ( Get-ItemProperty -Path "Registry::$gskey" ).'(default)'
if ( $gspath ) {
if ( $Debug ) {
Write-Host ( "Found GhostScript path in the registry: `"{0}`"" -f $gspath )
}
$gsprog = ( Get-ChildItem -Path "$gspath" -Filter 'gs*c.exe' -Recurse ).FullName
if ( $Debug ) {
Write-Host ( "Found GhostScript executable: `"{0}`"" -f $gsprog )
}
}
}
$gsprog
}
function Convert-PDF2Text( [string]$pdffile, [string]$textfile = '' ) {
if ( $Debug ) {
Write-Host ( "Trying to convert downloaded PDF aircraft registry database file `"{0}`" to plain text at {1}" -f $pdffile, ( Get-Date ) )
Write-Host "First, try and find GhostScript"
}
$gsexec = ( GetGhostScript )
if ( [string]::IsNullOrWhiteSpace( $gsexec ) ) {
if ( !$Quiet ) {
Write-Host "GhostScript not found on this computer"
}
$false
} else {
if ( $Debug ) {
Write-Host ( "GhostScript command line executable found: `"{0}`"" -f $gsexec )
}
if ( Test-Path -Path "$pdffile" -PathType 'Leaf' ) {
if ( $Debug ) {
Write-Host "PDF file found"
}
if ( [string]::IsNullOrWhiteSpace( $textfile ) ) {
$parentfolder = [System.IO.Directory]::GetParent( $pdffile )
$textfilename = [System.IO.Path]::GetFileNameWithoutExtension( $pdffile )
$newtextfile = ( Join-Path -Path "$parentfolder" -ChildPath "$textfilename.txt" )
$oldtextfile = ( Join-Path -Path "$parentfolder" -ChildPath "$textfilename.old" )
if ( $Debug ) {
Write-Host ( "New text file will be `"{0}`'" -f $newtextfile )
}
if ( Test-Path -Path "$newtextfile" -PathType 'Leaf' ) {
if ( $debug ) {
Write-Host "Text file already exist"
}
if ( Test-Path -Path $oldtextfile -PathType 'Leaf' ) {
if ( $Debug ) {
Write-Host ( "Deleting old text file `"{0}`"" -f $oldtextfile )
}
[System.IO.File]::Delete( $oldtextfile )
}
if ( $Debug ) {
Write-Host ( "Renaming existing text file `"{0}`" to `"{1}`"" -f $newtextfile, $oldtextfile )
}
[System.IO.File]::Move( $newtextfile, $oldtextfile )
}
} else {
$newtextfile = $textfile
if ( $Debug ) {
Write-Host ( "New text file will be `"{0}`'" -f $newtextfile )
}
}
if ( $Debug ) {
Write-Host ( "Actual conversion started at {0} using the command:" -f ( Get-Date ) )
Write-Host "`"$gsexec`" -sDEVICE=txtwrite -o `"$newtextfile`" `"$pdffile`""
}
( . "$gsexec" -sDEVICE=txtwrite -o "$newtextfile" "$pdffile" )
$true
} else {
if ( $Debug -or !$Quiet ) {
Write-Host ( "Downloaded PDF aircraft registry database file `"{0}`" not found" -f $pdffile )
}
$false
}
}
}
$OE_mark = $Registration.Substring( 3 )
$dbfile = ''
$dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'OE' )
if ( $Debug ) {
$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
}
if ( $ConvertPDF ) {
$ErrorActionPreference = 'SilentlyContinue'
$pdffile = ( Get-ChildItem -Path $dbfolder -Filter 'Gesamt_*_EN.pdf' | Sort-Object -Property 'Name' | Select-Object -Last 1 ).FullName
$ErrorActionPreference = 'Continue'
if ( $Debug ) {
Write-Host "-ConvertPDF switch used, convert latest PDF to plain text first"
Write-Host ( "Started search for local database (PDF format) in folder `"{0}`" at {1}" -f $dbfolder, ( Get-Date ) )
}
if ( Convert-PDF2Text $pdffile ) {
$pdffilename = [System.IO.Path]::GetFileNameWithoutExtension( $pdffile )
$dbfile = ( Join-Path -Path $dbfolder -ChildPath "$pdffilename.txt" )
}
} else {
if ( $Debug ) {
Write-Host ( "Started search for local database (text format) in folder `"{0}`" at {1}" -f $dbfolder, ( Get-Date ) )
}
$ErrorActionPreference = 'SilentlyContinue'
$dbfile = ( Get-ChildItem -Path $dbfolder -Filter 'Gesamt_*_EN.txt' | Sort-Object -Property 'Name' | Select-Object -Last 1 ).FullName
$ErrorActionPreference = 'Continue'
}
if ( [string]::IsNullOrWhiteSpace( $dbfile ) ) {
if ( $Quiet -and !$Debug ) {
exit 1
} else {
if ( $Debug ) {
Write-Host "Converted text file not found, looking for original PDF aircraf registry database file"
}
$textfilename = [System.IO.Path]::GetFileNameWithoutExtension( $dbfile )
$pdffile = ( Join-Path -Path $dbfolder -ChildPath "$textfilename.pdf" )
if ( Test-Path $pdffile -PathType 'Leaf' ) {
if ( $Debug ) {
Write-Host ( "Founf PDF file `"{0}`"" -f $pdffile )
}
$message = "A PDF database file was found, but it needs to be converted to text for this script to function.`n`nDo you want to convert the PDF file to a plain text file now?"
$title = "PDF to Text Conversion Required"
$buttons = [System.Windows.Forms.MessageBoxButtons]::YesNo
$answer = [System.Windows.Forms.MessageBox]::Show( $message, $title, $buttons )
if ( $answer = 'Yes' ) {
if ( Convert-PDF2Text $pdffile ) {
if ( $Debug ) {
Write-Host "PDF to text conversion completed"
}
} else {
if ( $Debug -or !$Quiet ) {
Write-Host "PDF to text conversion failed"
exit 1
}
}
} else {
if ( $Debug -or !$Quiet ) {
Write-Host "Please convert your PDF aircraft registry database file to plain text and try again"
}
exit 1
}
}
}
}
if ( $dbfile ) {
if ( $Debug ) {
Write-Host ( "Start searching {0} in {1} file at {1}" -f $OE_mark, $dbfile, ( Get-Date ) )
}
$pattern = "^\s{{16}}{0}\s" -f $OE_mark
$found = $false
( Get-Content -Path $dbfile ).Split( "`n" ) | ForEach-Object {
if ( !$found ) {
if ( $_ -match $pattern ) {
if ( $Debug ) {
Write-Host ( "Found a match at {0}" -f ( Get-Date ) )
}
$Manufacturer = $_.Substring( 40, 45 ).Trim( )
} elseif ( ![string]::IsNullOrWhiteSpace( $Manufacturer ) ) {
if ( $_.Length -gt 40 ) {
if ( ![string]::IsNullOrWhiteSpace( $_.Substring( 40, [Math]::Min( 45, $_.Length - 40 ) ) ) ) {
$Model = $_.Substring( 40, [Math]::Min( 45, $_.Length - 40 ) ).Trim( )
$found = $true
"{0}`t{1}`t{2}" -f $Registration, $Manufacturer, $Model
}
}
}
}
}
if ( $Debug ) {
Write-Host ( "Finished at {0} (elapsed time {1})`n`n" -f ( Get-Date ), $StopWatch.Elapsed )
$StopWatch.Stop( )
}
} else {
# No database text file found
if ( $Quiet ) {
if ( $Debug ) {
Write-Host ( "Downloaded Austrian aircraft register database file `"{0}`" not found" -f $dbfile )
}
exit 1
} else {
$message = "No downloaded Austrian 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://www.austrocontrol.at/en/aviation_agency/aircraft/aircraft_register/overview__supplement'
Start-Process $url
} else {
ShowHelp( 'No downloaded Austrian aircraft register database found, please download it and try again' )
}
}
}
page last modified: 2024-04-16; loaded in 0.0109 seconds