Last active
March 1, 2023 14:33
-
-
Save tommymaynard/b6297ed185310d5163847d852a8591fa to your computer and use it in GitHub Desktop.
Convert Alphanumeric String to the NATO Phonetic Alphabet Equivalent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
TechNet Contribution: Convert Alphanumeric String to the NATO Phonetic Alphabet Equivalent | |
Previous link: https://gallery.technet.microsoft.com/Convert-Alphanumeric-8c1d6a79 | |
Downloaded: 858 times (as of 05/21/2020) | |
There was a post in the TechNet Forum about needing a solution to convert a string into the NATO phonetic alphabet | |
equivalent (link). I really liked the project and quickly wrote out an advanced function in Windows PowerShell to | |
accompany the VBS solution that was also posted. Please let me know if there are any problems that need correcting. | |
The 1.3 version will speak the results, if the -Speak switch parameter is included when the function is run. | |
I feel confident that this will run on most, if not all, Windows Operating Systems even though it has only been tested | |
on Window 7 x64. | |
Tommy Maynard | |
Updated to version 1.3 on 2/24/2015 | |
Updated to version 1.2 on 8/21/2014 | |
#> | |
Function Convert-TMNatoAlphabet { | |
##### ** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ** | |
<# | |
.SYNOPSIS | |
Converts an alphanumeric string into the NATO Phonetic Alphabet equivalent. | |
.DESCRIPTION | |
The advanced function will convert an alphanumeric string into the NATO phonetic alphabet. | |
.PARAMETER String | |
This is the default, required parameter. It is the string that the advanced function will convert. | |
.EXAMPLE | |
Convert-TMNatoAlphabet -String '12abc3' | |
This example will convert the string, 12abc3, to its NATO phonetic alphabet equivalent. It will return, "One Two Alpha Bravo Charlie Three." | |
.EXAMPLE | |
Convert-TMNatoAlphabet -String '1p2h3-cc' | |
This example will attempt to convert the string, 1p2h3-cc, to its NATO phonetic alphabet equivalent. Since it contains an invalid character (-), it will return, "String contained illegal character(s)." | |
.EXAMPLE | |
Convert-TMNatoAlphabet '1ph3cc' | |
This example will convert the string, 1ph3cc, to its NATO phonetic alphabet equivalent. It will return, "One Papa Hotel Three Charlie Charlie." | |
.EXAMPLE | |
Convert-TMNatoAlphabet '1ph3cc' -Speak | |
This example will convert the string, 1ph3cc, to its NATO phonetic alphabet equivalent. It will return, "One Papa Hotel Three Charlie Charlie." In addition, it will speak the results. | |
.NOTES | |
NAME: Convert-TMNatoAlphabet | |
AUTHOR: Tommy Maynard | |
LASTEDIT: 08/21/2014 | |
VERSION 1.1 | |
-Changed seperate alpha and numeric hashes into one, alphanumeric hash (numbers are being stored as strings). | |
VERSION 1.2 | |
-Edited the logic that handles the conversion (no need for If and nested If - Initial If handles a-z 0-9 check). | |
-Added string cleanup inside If statement. | |
VERSION 1.3 | |
-Added switch parameter to speak the results, as well. | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True,Position=0)] | |
[string]$String, | |
[switch]$Speak | |
) | |
Begin { | |
Write-Verbose -Message 'Creating alphanumeric hash table' | |
$Hash = @{'A'=' Alpha ';'B'=' Bravo ';'C'=' Charlie ';'D'=' Delta ';'E'=' Echo ';'F'=' Foxtrot ';'G'=' Golf ';'H'=' Hotel ';'I'=' India ';'J'=' Juliet ';'K'=' Kilo ';'L'=' Lima ';'M'=' Mike ';'N'=' November ';'O'=' Oscar ';'P'=' Papa ';'Q'=' Quebec ';'R'=' Romeo ';'S'=' Sierra ';'T'=' Tango ';'U'=' Uniform ';'V'=' Victory ';'W'=' Whiskey ';'X'=' X-ray ';'Y'=' Yankee ';'Z'=' Zulu ';'0'=' Zero ';'1'=' One ';'2'=' Two ';'3'=' Three ';'4'=' Four ';'5'=' Five ';'6'=' Six ';'7'=' Seven ';'8'=' Eight ';'9'=' Nine '} | |
} # End Begin | |
Process { | |
Write-Verbose -Message 'Checking string for illegal charcters' | |
If ($String -match '^[a-zA-Z0-9]+$') { | |
Write-Verbose -Message 'String does not have any illegal characters' | |
$String = $String.ToUpper() | |
Write-Verbose -Message 'Creating converted string' | |
For ($i = 0; $i -le $String.Length; $i++) { | |
[string]$Character = $String[$i] | |
$NewString += $Hash.Get_Item($Character) | |
} | |
Write-Verbose -Message 'Cleaning up converted string' | |
$NewString = ($NewString.Trim()).Replace(' ',' ') | |
Write-Output $NewString | |
If ($Speak) { | |
Add-Type -AssemblyName System.Speech | |
$Voice = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer | |
$Voice.Speak($NewString) | |
} | |
} Else { | |
Write-Output -Verbose 'String contained illegal character(s).' | |
} | |
} # End Process | |
} # End Function |
Author
tommymaynard
commented
May 21, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment