我们正在尝试将CountryCode
转换为人类可读的String
。
代码:
$OS = Get-CimInstance -ClassName Win32_OperatingSystem
$OS | Select-Object CountryCode, OSLanguage,
@{N = 'OSDefaultLanguage'; E = {New-Object System.Globalization.CultureInfo([Int]$_.OSLanguage)}},
@{N = 'OSCountryCode'; E = {New-Object System.Globalization.CultureInfo([Int]$_.CountryCode)}}
在上面的示例中,属性OSCountryCode
是我们需要的。但它返回的值ar
为Argentina
,但它应根据值United States
的{{3}}返回1
。
如何将值1
正确转换为US
或类似内容?
答案 0 :(得分:1)
$OS = Get-CimInstance -ClassName Win32_OperatingSystem
$Culture = [System.Globalization.CultureInfo]::GetCultures("SpecificCultures") | Where {$_.LCID -eq $OS.OSLanguage}
$RegionInfo = New-Object System.Globalization.RegionInfo $Culture.Name
$OS | Select-Object CountryCode, OSLanguage,
@{N = 'OSDefaultLanguage'; E = {New-Object System.Globalization.CultureInfo([Int]$_.OSLanguage)}},
@{N = 'OSCountryCode'; E = {$RegionInfo.TwoLetterISORegionName}},
@{N = 'OSCountryName'; E = {$RegionInfo.DisplayName}}