将Win32_OperatingSystem的CountryCode转换为国家/地区字符串

时间:2017-10-31 10:41:44

标签: powershell wmi

我们正在尝试将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是我们需要的。但它返回的值arArgentina,但它应根据值United States的{​​{3}}返回1

如何将值1正确转换为US或类似内容?

1 个答案:

答案 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}}