<# .SYNOPSIS Search Pascal Brugier's online Belgian aircraft registration database for a Belgian aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited) .DESCRIPTION Run this script with an aircraft registration as its only parameter (see examples section). The script will try to find the registration in Pascal Brugier's online Belgian aircraft registration database. If a match is found, the script will display a tab-delimited string with the registration and the manufacturer/model (). If the script was started by another PowerShell script, the calling PowerShell script may also read the manufacturer/model from the variable $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/model. Get-Help './AirRegOOCmd.ps1' -Examples will show 2 examples of this script being called by another script. .PARAMETER Registration A valid Belgian aircraft registration, i.e. O-**** or OO-**** .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 with , and a variable $Model set to manufacturer AND model .EXAMPLE . ./AirRegOOCmd.ps1 "OO-AHZ" Will return tab-delimited string "OO-AHZSABCA HANDLEY PAGE W.8F" and set variable $Model to "SABCA HANDLEY PAGE W.8F" .EXAMPLE "OO-AHZ" | . ./AirRegOOCmd.ps1 Will also return tab-delimited string "OO-AHZSABCA HANDLEY PAGE W.8F" and set variable $Model to "SABCA HANDLEY PAGE W.8F" .EXAMPLE . ./AirRegOOCmd.ps1 -Version -Verbose Will return the full script path, version and last modified/release date .EXAMPLE . ./AirRegOOCmd.ps1 -Version Will return the script version .EXAMPLE Create and run the following PowerShell script: =============================================================== $Registration = 'OO-AHZ' ; $Model = '' [void] ( . "$PSScriptRoot\AirRegOOCmd.ps1" -Registration $Registration ) Write-Host ( "Registration : {0}`nModel : {1}" -f $Registration, $Model ) =============================================================== Besides setting variable $Model to "SABCA HANDLEY PAGE W.8F", it will return: Registration : OO-AHZ Model : SABCA HANDLEY PAGE W.8F .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 . ./AirRegOOCmd.ps1 OO-AHZ') DO ( ECHO Registration : %%A ECHO Model : %%B ) =============================================================== It will return: Registration : OO-AHZ Model : SABCA HANDLEY PAGE W.8F .LINK Script written by Rob van der Woude: https://www.robvanderwoude.com/ .LINK Pascal Brugier's online Belgian aircraft registration database: http://www.brugier.com/textes/txt/oo-aaa.txt .LINK Capture -Debug and -Verbose parameter by mklement0 on StackOverflow.com: https://stackoverflow.com/a/48643616 #> param ( [parameter( ValueFromPipeline )] [ValidatePattern("(^\s*$|[\?/]|^O{1,2}-[A-Z\d]{2,4}$)")] [string]$Registration, [switch]$Quiet, [switch]$Help, [switch]$Version ) $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 } 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 } $dburl = 'http://www.brugier.com/textes/txt/oo-aaa.txt' $pattern = ( '^{0}\s[^\n\r]{{1}}' -f $Registration, ( 60 - $Registration.Length ).ToString( ) ) if ( $Debug ) { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew( ) Write-Host ( "Starting online search for {0} at {1}" -f $Registration, ( Get-Date ) ) } $ProgressPreference = 'SilentlyContinue' $table = ( Invoke-WebRequest -Uri $dburl ).Content $ProgressPreference = 'Continue' if ( $table.Length -lt 100 ) { if ( !$Quiet ) { Write-Host "Error retrieving Belgian aircraft registration list" } exit 1 } $linescount = $table.Split( [environment]::NewLine ).Length if ( $Debug ) { Write-Host ( "Registration list retrieved, {0} lines" -f $linescount ) } if ( $linescount -lt 1000 ) { if ( !$Quiet ) { Write-Host ( "Error in retrieved Belgian aircraft registration list: only {0} lines" -f $linescount ) } exit 1 } $matchfound = $false $linescount = 0 $table.Split( [environment]::NewLine ) | ForEach-Object { if ( !$matchfound ) { if ( $Model -ne '' ) { $matchfound = $true if ( $Debug ) { Write-Host ( "Match found in line {0}" -f $linescount ) } } if ( $_.StartsWith( "$Registration " ) ) { $Model = $_.Substring( $Registration.Length ).Trim( ) $pattern = "\s{2,}.*$" $Model = [System.Text.RegularExpressions.Regex]::Replace( $Model, $pattern, "" ) } $linescount += 1 } } if ( $Debug ) { Write-Host ( "Ended online search for {0} at {1} (elapsed time {2})" -f $Registration, ( Get-Date ), $StopWatch.Elapsed ) $StopWatch.Stop( ) } if ( !$matchfound ) { if ( !$Quiet ) { Write-Host "Aircraft registration for $Registration not found" } } else { "{0}`t`t{1}" -f $Registration, $Model }