question

ChristopherJack-1763 avatar image
0 Votes"
ChristopherJack-1763 asked ChristopherJack-1763 commented

2 Digit Country Codes generation in Powershell

Hi,

I am requiring to output a 2 digit country code based on a country.

Array would be something like

 $array += [[[[Austria],[AU]],[[Brazil],[BZ]]]]

So i would expect it to be something like

 if ($array -match 'Brazil'){ it returns BZ}

What would be the best way of going about that?

Thanks for help



windows-server-powershell
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Just a FYI: The two-letter country code for Austria is "AT", not "AU" -- Australia is "AU". ;-)

2 Votes 2 ·

lol, just pulled it out my head as an example

0 Votes 0 ·
RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered ChristopherJack-1763 commented

Don't use an array, use a hash instead:

 $hash = @{Austria='AU'; Brazil='BZ'}
 $CountryNames = 'Austria','Brazil','Canada'
 $CountryNames |
     ForEach-Object{
         $CC = $hash[$_]
         Write-Host "Country name '$_' has the code '$CC'"   # doesn't report an error, just an empty country code
     }
 # -- or
 $CountryNames |
     ForEach-Object{
         if($hash.ContainsKey($_)){
             $CC = $hash[$_]
             Write-Host "Country name '$_' has the code '$CC'"
         }
         Else{
             Write-Host "Country name '$_' is not in lookup table"
         }
     }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Rich - Much appreciated

0 Votes 0 ·
RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered ChristopherJack-1763 commented

If you'd rather use what data are present in the Windows O/S instead of building/maintaining your own translation table, give this a try. The $hash variable at the end is what you want:

 $AllCultures = [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures) # !AllCultures
    
 $AllCultures |
     ForEach-Object{
         $dn = $_.DisplayName.Split("(|)");
         $RegionInfo = New-Object System.Globalization.RegionInfo $PsItem.name;
         [pscustomobject]@{
                 Name = $RegionInfo.Name;
                 EnglishName = $RegionInfo.EnglishName;
                 TwoLetterISORegionName = $RegionInfo.TwoLetterISORegionName;
                 GeoId = $RegionInfo.GeoId;
                 ISOCurrencySymbol = $RegionInfo.ISOCurrencySymbol;
                 CurrencySymbol = $RegionInfo.CurrencySymbol;
                 IsMetric = $RegionInfo.IsMetric;
                 LCID = $PsItem.LCID;
                 Lang = $dn[0].Trim();
                 Country = $dn[1].Trim();
             }
     } |
         # get list of unique country names and ISO codes in Windows
         Select-Object -Unique -prop TwoLetterISORegionName,EnglishName | 
             Sort-Object TwoLetterISORegionName |
                 ForEach-Object{
                     $hash[$_.EnglishName] = $_.TwoLetterISORegionName
                 }

All the information in the PSCustomObject isn't necessary. It doesn't hurt to know it's there, though.

The code was shamelessly purloined (and slightly modified) from here: powershell-get-list-of-two-letter-country-iso-3166-code-alpha-2-currency-language-and-more


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Rich, its to go into a function so will have a think of the best way to do it..

$Delivery += "DELIVERY",$A[3],"",( Get-Random -Minimum 10000000000000000000 -Maximum 20000000000000000000 ).ToString('00000000000000000000'),"",$example[1].'Shipping Address Name',$example[1].'Shipping Address Line 1',$example[1].'Shipping Address Line 2',$example[1].'Shipping Address Suburb', $example[1].'Shipping Address City',$example[1].'Shipping Address State',$example[1].'Shipping Address Zip Code', Shipping Address Country 2 Letter ISO Code,"","",EM,$example[1].'Billing Address Email',$example[1].'TE,Billing Address Phone Number',"","","","","","","","","","","","","","","","","",""

0 Votes 0 ·

Okay . . . I'm not following. You're loading an array with values, but the quoting in the code is incorrect. If you unwind it there are missing quotation marks around "Shipping Address Country 2 Letter ISO Code".

 $Delivery += "DELIVERY",
 $A[3],
 "",
 ( Get-Random -Minimum 10000000000000000000 -Maximum 20000000000000000000 ).ToString('00000000000000000000'),
 "",
 $example[1].'Shipping Address Name',
 $example[1].'Shipping Address Line 1',
 $example[1].'Shipping Address Line 2',
 $example[1].'Shipping Address Suburb', 
 $example[1].'Shipping Address City',
 $example[1].'Shipping Address State',
 $example[1].'Shipping Address Zip Code',
 Shipping Address Country 2 Letter ISO Code,
 "",
 etc.

What's has this to do with the code samples I posted?????

0 Votes 0 ·

getting error

Cannot convert value "World" to type "System.Int32". Error: "Input string was not in a correct format."
At D:\Powershell\EDI.ps1:31 char:22
+ ... $hash[$.EnglishName] = $.TwoLetterISORegionName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger


Cannot convert value "Caribbean" to type "System.Int32". Error: "Input string was not in a correct format."
At D:\Powershell\EDI.ps1:31 char:22
+ ... $hash[$.EnglishName] = $.TwoLetterISORegionName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger

0 Votes 0 ·

Again, I'm lost.

The code (assuming you're using the example that loads the has from systems localization data) "$hash[$<undebar>.EnglishName] = $<underbar>.TwoLetterISORegionName" doesn't involve casting anything to an integer value.

Are you sure that you don't have a missing quotation mark somewhere??? Using an IDE that does checking as you're writing the code (such as VS Code, or Visual Studio) can help you there.

1 Vote 1 ·

Ah thanks Rich, Ill check them out. currently using notepad.

0 Votes 0 ·