<# .SYNOPSIS Search the online Icelandic aircraft registration database for an Icelandic 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 the online Icelandic aircraft registry. 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 $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 './AirRegPHCmd.ps1' -Examples will show 2 examples of this script being called by another script. .PARAMETER Registration A valid Icelandic aircraft registration, i.e. TF-xxx (where each x is a single letter) .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 Debug Show some progress messages .OUTPUTS A tab-delimited string with , and a variable $Model set to manufacturer AND model .EXAMPLE . ./AirRegTFmd.ps1 "TF-NPK" Will return tab-delimited string "TF-NPKDouglas C47A" and set variable $Model to "Douglas C47A" .EXAMPLE "TF-NPK" | . ./AirRegTFCmd.ps1 Will also return tab-delimited string "TF-NPKDouglas C47A" and set variable $Model to "Douglas C47A" .EXAMPLE Create and run the following PowerShell script: =============================================================== $Registration = 'TF-NPK' ; $Model = '' [void] ( . "$PSScriptRoot\AirRegTFCmd.ps1" -Registration $Registration ) Write-Host ( "Registration : {0}`nModel : {1}" -f $Registration, $Model ) =============================================================== Besides setting variable $Model to "Douglas C47A", it will return: Registration : TF-NPK Model : Douglas C47A .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 . ./AirRegTFCmd.ps1 TF-NPK') DO ( ECHO Registration : %%A ECHO Model : %%B ) =============================================================== It will return: Registration : TF-NPK Model : Douglas C47A .LINK Script written by Rob van der Woude: https://www.robvanderwoude.com/ .LINK Icelandic Transport Authority online aircraft registry: https://www.icetra.is/aviation/aircraft/register/ #> param ( [parameter( ValueFromPipeline )] [ValidatePattern("(^\s*$|[\?/]|^TF-[A-Z]{3}$)")] [string]$Registration, [switch]$Quiet, [switch]$Help ) $progver = "1.00" $Registration = $Registration.ToUpper( ) $Manufacturer = '' $Model = '' [bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) ) function ShowHelp( $message = '' ) { if ( !$Quiet ) { if ( $errormessage ) { Write-Host Write-Host "Error: " -ForegroundColor Red -NoNewline Write-Host $errormessage } Write-Host Write-Host ( "AirRegTFCmd.ps1, Version {0}" -f $progver ) Write-Host "Search online Icelandic aircraft registration database for a registation" Write-Host Write-Host "Usage: " -NoNewline Write-Host ". ./AirRegTFCmd.ps1 [-Registration] TF-*** [-Quiet] [-Debug] [-Help]" -ForegroundColor White Write-Host Write-Host "Where: " -NoNewline Write-Host "TF-*** " -NoNewline -ForegroundColor White Write-Host "is a valid Icelandic aircraft registration, e.g. TF-NPK" Write-Host " -Quiet " -NoNewline -ForegroundColor White Write-Host "all errors are ignored and no error messages displayed" Write-Host " -Debug " -NoNewline -ForegroundColor White Write-Host "shows some progress messages" Write-Host " -Help " -NoNewline -ForegroundColor White Write-Host "shows this help screen" Write-Host Write-Host "Notes: This script uses the Icelandic aircraft registration database at:" Write-Host " https://www.icetra.is/aviation/aircraft/register/" -ForegroundColor DarkGray Write-Host " The result, if any, of the search is displayed as tab-delimited text:" Write-Host " " Write-Host " Besides its screen output, this script will also set the `$Model" Write-Host " variable with the database search result." Write-Host " Run " -NoNewline Write-Host "Get-Help `"./AirRegTFCmd.ps1`" -Examples " -NoNewline -ForegroundColor White Write-Host "for some examples of" Write-Host " `"nesting`" this script in other PowerShell or batch scripts." Write-Host " Return code (`"ErrorLevel`") 1 in case of errors, otherwise 0." Write-Host Write-Host "Written by Rob van der Woude" Write-Host "https://www.robvanderwoude.com" } exit 1 } if ( $Help -or $Registration -match "(^\s*$|^-|\?|/)" ) { ShowHelp exit 1 } $dburl = 'https://www.icetra.is/aviation/aircraft/register/' #$regurl = "https://www.icetra.is/aviation/aircraft/register?aq=$Registration" $pattern = ( '(?sm)]*>({0})[^<]*[^<]*[^<]*([^<]+)' -f $Registration ) if ( $Debug ) { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew( ) Write-Host ( "Starting online search for {0} at {1}" -f $Registration, ( Get-Date ) ) } $ProgressPreference = 'SilentlyContinue' $html = ( Invoke-WebRequest -Uri $dburl -Method 'Merge' ).Content $ProgressPreference = 'Continue' $Matches = ( $html | Select-String -Pattern $pattern -AllMatches ).Matches if ( $Debug ) { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew( ) Write-Host ( "Ended online search for {0} at {1} with {2} matches found (elapsed time {3})" -f $Registration, ( Get-Date ), $Matches.Count, $StopWatch.Elapsed ) $StopWatch.Stop( ) } if ( $Matches.Count -eq 0 ) { if ( !$Quiet ) { Write-Host "Aircraft registration for $Registration not found" } } else { $Model = $Matches[0].Groups[2] "{0}`t{1}" -f $Matches[0].Groups[1], $Model }