Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregopenskycmd.ps

(view source code of airregopenskycmd.ps as plain text)

  1. <#
  2. .SYNOPSIS
  3. Search the downloaded OpenSky Network aircraft registration database for an aircraft registation and if found, return the aircraft manufacturer and model (tab-delimited)
  4.  
  5. .DESCRIPTION
  6. First create a subdirectory 'OpenSkyNetwork' in this script's parent directory.
  7. Next download the OpenSky Network aircraft registration database (see links section), we will be using aircraftDatabase.csv only for this script.
  8. Run this script with an aircraft registration as its only parameter (see examples section).
  9. The script will try to find the registration in the downloaded aircraft registry.
  10. 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>).
  11. 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.
  12. 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.
  13. Get-Help './AirRegOpenSkyCmd.ps1' -Examples will show 2 examples of this script being called by another script.
  14.  
  15. .PARAMETER Registration
  16. A valid aircraft registration, e.g. N1944S, OK-NUL or OO-CAT
  17.  
  18. .PARAMETER Quiet
  19. Ignore all errors and do not display any error messages; in case of errors, just terminate with return code 1
  20.  
  21. .PARAMETER NoBreak
  22. Do not break the search loop after a match is found.
  23. On average searches take about twice as long when using this switch; hence it is recommended to use it only if:
  24.   -*-  this script is called by another PowerShell script
  25. and:
  26.   -*-  the calling PowerShell script uses the variables $Manufacturer and $Model set by this script
  27.  
  28. .PARAMETER Help
  29. Show the script's help screen
  30.  
  31. .PARAMETER Version
  32. Show this script's version number; if combined with -Verbose show full script path, version number and last/modified/release date
  33.  
  34. .PARAMETER Debug
  35. Show some progress messages; implies -NoBreak
  36.  
  37. .OUTPUTS
  38. 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
  39. If no match is found: False
  40.  
  41. .EXAMPLE
  42. . ./AirRegOpenSkyCmd.ps1 "OO-CAT"
  43. Will return tab-delimited string "OO-CAT<tab>Amateur Built<tab>Jodel D.112"
  44.  
  45. .EXAMPLE
  46. "OO-CAT" | . ./AirRegOpenSkyCmd.ps1 -NoBreak
  47. 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"
  48.  
  49. .EXAMPLE
  50. . ./AirRegOpenSkyCmd.ps1 -Version -Verbose
  51. Will return the full script path, version and last modified/release date
  52.  
  53. .EXAMPLE
  54. . ./AirRegOpenSkyCmd.ps1 -Version
  55. Will return the script version
  56.  
  57. .EXAMPLE
  58. . ./AirRegOpenSkyCmd.ps1 OO-CAT -Debug
  59. Will return:
  60.  
  61. Started searching for OO-CAT in ".\OpenSkyNetwork\aircraftDatabase.csv" at <date> <time>
  62. Found a match at <date> <time>
  63. OO-CAT  Amateur Built   Jodel D.112
  64. Finished at <date> <time> (time elapsed <elapsed time>)
  65.  
  66. .EXAMPLE
  67. Create and run the following PowerShell script:
  68. ===============================================================
  69. $Registration = 'OO-CAT' ; $Model = ''
  70. [void] ( . "$PSScriptRoot\AirRegOpenSkyCmd.ps1" -Registration $Registration -NoBreak )
  71. Write-Host ( "Registration : {0}`nManufacturer : {1}`nModel        : {2}" -f $Registration, $Manufacturer, $Model )
  72. ===============================================================
  73.  
  74. Besides setting variables $Manufacturer to "Amateur Built" and $Model to "Jodel D.112", it will return:
  75.  
  76. Registration : OO-CAT
  77. Manufacturer : Amateur Built
  78. Model        : Jodel D.112
  79.  
  80. .EXAMPLE
  81. Create and run the following batch file:
  82. ===============================================================
  83. REM Note that there should only be a TAB and nothing else between delims= and the doublequote
  84. FOR /F "tokens=1-3 delims=	" %%A IN ('powershell . ./AirRegOpenSkyCmd.ps1 OO-CAT') DO (
  85. 	ECHO Registration : %%A
  86. 	ECHO Manufacturer : %%B
  87. 	ECHO Model        : %%C
  88. )
  89. ===============================================================
  90.  
  91. It will return:
  92.  
  93. Registration : OO-CAT
  94. Manufacturer : Amateur Built
  95. Model        : Jodel D.112
  96.  
  97. .LINK
  98. Script written by Rob van der Woude:
  99. https://www.robvanderwoude.com/
  100.  
  101. .LINK
  102. OpenSky Network aircraft registration database:
  103. https://opensky-network.org/datasets/metadata/
  104.  
  105. .LINK
  106. Capture -Debug and -Verbose parameter by mklement0 on StackOverflow.com:
  107. https://stackoverflow.com/a/48643616
  108. #>
  109.  
  110. param (
  111. 	[parameter( ValueFromPipeline )]
  112. 	[ValidatePattern("(^\s*$|[\?/]|^\w[\w-]{2,3}\w+)")]
  113. 	[string]$Registration,
  114. 	[switch]$Quiet,
  115. 	[switch]$NoBreak,
  116. 	[switch]$Help,
  117. 	[switch]$Version
  118. )
  119.  
  120. $progver = "1.00"
  121.  
  122. $Registration = $Registration.ToUpper( )
  123. $Manufacturer = ''
  124. $Model = ''
  125. [bool]$Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  126. [bool]$Verbose = ( $PSBoundParameters.ContainsKey( 'Verbose' ) )
  127.  
  128. if ( $Version ) {
  129. 	if ( $Verbose ) {
  130. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  131. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  132. 			"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  133. 		} else {
  134. 			# if last modified time is not equal to program version, the script has been tampered with
  135. 			"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  136. 		}
  137. 	} else {
  138. 		$progver
  139. 	}
  140. 	exit 0
  141. }
  142.  
  143. if ( $Help -or [string]::IsNullOrWhiteSpace( $Registration ) -or ( $Registration -match "[/\?]" ) ) {
  144. 	Clear-Host
  145. 	Write-Host ( "`"{0}`", Version {1}" -f $PSCommandPath, $progver ) -NoNewline
  146. 	$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  147. 	if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  148. 		Write-Host ", release date " -NoNewline
  149. 	} else {
  150. 		# if last modified time is not equal to program version, the script has been tampered with
  151. 		Write-Host ", last modified date " -NoNewline
  152. 	}
  153. 	Write-Host $lastmod.ToString( "yyyy-MM-dd" )
  154. 	Write-Host
  155. 	Get-Help $PSCommandPath -Full
  156. 	exit -1
  157. }
  158.  
  159. $found = $false
  160. $dbfolder = ( Join-Path -Path $PSScriptRoot -ChildPath 'OpenSkyNetwork' )
  161. if ( Test-Path -Path $dbfolder -PathType 'Container' ) {
  162. 	$dbfile = ( Join-Path -Path $dbfolder -ChildPath 'aircraftDatabase.csv' )
  163. 	if ( Test-Path -Path $dbfile -PathType 'Leaf' ) {
  164. 		if ( $Debug ) {
  165. 			$StopWatch = [System.Diagnostics.Stopwatch]::StartNew( )
  166. 			Write-Host ( "Started searching for {0} in `"{1}`" at {2}" -f $Registration, $dbfile, ( Get-Date ) )
  167. 		}
  168. 		( Get-Content $dbfile ).Split( "`n" ) | Select-String -Pattern "`"$Registration`"" -AllMatches | ForEach-Object {
  169. 			if ( !$found ) {
  170. 				$fields = $_.ToString( ).Split( ',' )
  171. 				if ( $fields[1] -match "`"$Registration`"" ) {
  172. 					if ( $Debug ) {
  173. 						Write-Host ( "Found a match at {0}" -f ( Get-Date ) )
  174. 					}
  175. 					$Manufacturer = $fields[3]
  176. 					$Model = $fields[4]
  177. 					( "{0}`t{1}`t{2}" -f $fields[1], $Manufacturer, $Model ) -replace '"',''
  178. 					if ( $Debug ) {
  179. 						Write-Host ( ( "{0}`t{1}`t{2}" -f $fields[1], $Manufacturer, $Model ) -replace '"','' )
  180. 					}
  181. 					$found = $true
  182. 					if ( -not $NoBreak ) {
  183. 						if ( $Debug ) {
  184. 							Write-Host ( "Finished at {0} (time elapsed {1})" -f ( Get-Date ), $StopWatch.Elapsed )
  185. 							$StopWatch.Stop( )
  186. 						} else {
  187. 							exit 0
  188. 						}
  189. 					}
  190. 				}
  191. 			}
  192. 		}
  193. 		if ( $Debug ) {
  194. 			Write-Host ( "Finished at {0} (time elapsed {1})" -f ( Get-Date ), $StopWatch.Elapsed )
  195. 			$StopWatch.Stop( )
  196. 		}
  197. 	} else {
  198. 		if ( -not $Quiet ) {
  199. 			Write-Host ( "Database file `"{0}`" not found" -f $dbfile )
  200. 		}
  201. 	}
  202. } else {
  203. 	if ( -not $Quiet ) {
  204. 		Write-Host ( "Folder `"{0}`" not found" -f $dbfolder )
  205. 	}
  206. }
  207. if ( !$found ) {
  208. 	$false
  209. }
  210.  

page last uploaded: 2019-01-21, 22:48