暂时将powershell语言更改为英语?

2024-01-07

我编写了一些使用系统(powershell)命令输出的软件,但没有预见到对于英语以外的语言,输出会有所不同。

有没有办法暂时将Powershell中的语言更改为英语就这一个,单个 powershell 会话?

Notes

  • 如果它很重要,我希望运行的特定 powershell 代码是netstat -n -a

  • 我遇到了一些更改 powershell 语言的方法(例如here https://social.technet.microsoft.com/Forums/exchange/en-US/6ed8f2d9-082a-4c86-8066-e9b2391f5dff/how-to-change-language-in-powershell-to-english?forum=winserverpowershell, here https://community.spiceworks.com/topic/560456-how-to-change-powershell-script-errors-to-english)。但我要小心不要永久改变它! (那会很糟糕)


  • (a) For 外部程序例如netstat.exe,不幸的是有no(据我所知)更改 UI 语言的方法会议中:

    • 在 Windows Server 2012 / Windows 8 及更高版本上,Set-WinUILanguageOverride https://learn.microsoft.com/en-us/powershell/module/international/set-winuilanguageoverride cmdlet让你(持续地)更改当前用户的系统范围 UI 语言,但这仅在future登录会话 - 也就是说,需要注销并重新登录或重新启动.

    • 顺便说一句:在 Windows Server 2012 / Windows 8 及更高版本上,还有Set-Culture https://learn.microsoft.com/en-us/powershell/module/international/set-culturecmdlet,但它的目的是not改变UI文化(显示语言),但仅限于特定文化设置,例如日期、数字和货币格式。它也改变了设置坚持不懈地对于当前用户,但只需要一个新的session(过程)以使更改生效。

  • (b) For PowerShell 命令 and .NET 类型, there is会话中(非持久)解决方案- 假设命令具有文化意识并附带本地化字符串:

    • Set [cultureinfo]::CurrentUICulture https://learn.microsoft.com/en-US/dotnet/api/System.Globalization.CultureInfo.CurrentUICulture(暂时)到所需的区域性名称(使用[cultureinfo]::GetCultures('SpecificCultures') https://learn.microsoft.com/en-US/dotnet/api/system.globalization.cultureinfo.getcultures查看所有预定义的);例如。,[cultureinfo]::CurrentUICulture = 'en-US'

      • 作为补充,您可能需要设置[cultureinfo]::CurrentCulture https://learn.microsoft.com/en-US/dotnet/api/System.Globalization.CultureInfo.CurrentCulture(注意缺少的UI部分),它也确定特定于区域性的数字、日期……格式。
      • 在旧版本的 PowerShell / .NET 中,您必须将这些属性设置为[System.Threading.Thread]::CurrentThread https://learn.microsoft.com/en-US/dotnet/api/System.Threading.Thread.CurrentThread反而;例如。,
        [System.Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'
    • See the 底部 for 辅助函数Use-Culture包装此功能以临时执行具有不同文化的代码;这是一个示例调用 与文化敏感的Get-LocalGroupMember https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localgroupmember cmdlet:

      # Try with values other than "en-US", e.g. "fr-FR" to see localized
      # values in the "ObjectClass" output column.
      Use-Culture en-US { Get-LocalGroupMember Administrators } 
      
    • An 特别示例,如果您不想定义辅助函数(此处仅更改 UI 区域性):

      & {
        $prev=[cultureinfo]::CurrentUICulture
        [cultureinfo]::CurrentUICulture='en-US'
        Get-LocalGroupMember Administrators
        [cultureinfo]::CurrentUICulture=$prev
      }
      

Caveats:

  • PowerShell [核心]本身就是not尚未本地化,从 v7.2.x 开始;正在跟踪进展情况GitHub 问题 #666 https://github.com/PowerShell/PowerShell/issues/666;但是,下面的解决方案确实适用于附带本地化消息和帮助内容的第三方模块,以及与平台 API 对话的特定于 Windows 的模块,例如Microsoft.PowerShell.LocalAccounts https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/模块,其Get-LocalGroupMember上面的示例中使用了 cmdlet。

  • 由于一个bug in Windows PowerShell(PowerShell [核心] v6+ 是not做作的),会话中更改为[cultureinfo]::CurrentUICulture and [cultureinfo]::CurrentCulture are 自动重置在命令提示符下,每当命令完成执行时;但是,对于给定的脚本,更改对整个脚本及其被调用者仍然有效 - 请参阅这个答案 https://stackoverflow.com/a/56922526/45375.


退一步说:

我编写了一些使用系统(powershell)命令输出的软件,但没有预见到对于英语以外的语言,输出会有所不同。

这正是为什么通常值得寻找PowerShell 原生解决方案而不是调用外部程序:

而不必解析 - 可能是本地化的 -text,与netstat.exe,例如,PowerShell 命令返回objects您可以以独立于文化的方式可靠地访问其属性。

具体来说,马蒂亚斯·R·杰森 https://stackoverflow.com/users/712649/mathias-r-jessen建议看看Get-NetTCPConnection https://learn.microsoft.com/en-us/powershell/module/nettcpip/get-nettcpconnection作为 PowerShell 的替代品netstat.exe(适用于 Windows Server 2012 / Windows 8 及更高版本)。


功能Use-Culture的源代码:

注意:该代码非常感谢改编自这篇令人尊敬的博客文章 https://rkeithhill.wordpress.com/2009/10/21/windows-powershell-2-0-string-localization/;它被设计

# Runs a script block in the context of the specified culture, without changing 
# the session's culture persistently.
# Handy for quickly testing the behavior of a command in the context of a different culture.
# Example: 
#   Use-Culture fr-FR { Get-Date }
function Use-Culture
{    
  param(
    [Parameter(Mandatory)] [cultureinfo] $Culture,
    [Parameter(Mandatory)] [scriptblock] $ScriptBlock
  )
  # Note: In Windows 10, a culture-info object can be created from *any* string.
  #        However, an identifier that does't refer to a *predefined* culture is 
  #        reflected in .LCID containing 4096 (0x1000)
  if ($Culture.LCID -eq 4096) { Throw "Unrecognized culture: $($Culture.DisplayName)" }

  # Save the current culture / UI culture values.
  $PrevCultures = [Threading.Thread]::CurrentThread.CurrentCulture, [Threading.Thread]::CurrentThread.CurrentUICulture

  try {
    # (Temporarily) set the culture and UI culture for the current thread.
    [Threading.Thread]::CurrentThread.CurrentCulture = [Threading.Thread]::CurrentThread.CurrentUICulture = $Culture

    # Now invoke the given code.
    & $ScriptBlock

  }    
  finally {
    # Restore the previous culture / UI culture values.
    [Threading.Thread]::CurrentThread.CurrentCulture = $PrevCultures[0]
    [Threading.Thread]::CurrentThread.CurrentUICulture = $PrevCultures[1]
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

暂时将powershell语言更改为英语? 的相关文章

随机推荐