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

标签 powershell

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

有没有办法将 Powershell 中的语言临时更改为英语,仅用于那个单一的 Powershell session ?

笔记

  • 如果它很重要,我希望运行的特定 powershell 代码是 netstat -n -a
  • 我遇到了一些更改 powershell 语言的方法(例如 herehere )。但我要小心不要永久更改它! (那会很糟糕)
  • 最佳答案

  • (一) 对于外部程序,例如 netstat.exe ,不幸的是(据我所知)无法在 session 中更改 UI 语言 :
  • 在 Windows Server 2012/Windows 8 及更高版本上, Set-WinUILanguageOverride 小命令 允许您(持续地)更改当前用户的系统范围的 UI 语言 , 但这只在以后的登录 session 中生效 - 即 需要注销并重新启动或重新启动 .
  • 顺便说一句:在 Windows Server 2012/Windows 8 及更高版本上,还有 Set-Culture cmdlet,但它的 目的不是改变 UI 文化(显示语言) , 但仅限于特定文化 日期、数字和货币格式等设置 .它也会持续更改当前用户的设置,但只需要一个新的 session (进程)才能使更改生效。

  • (二) 对于 PowerShell 命令和 .NET 类型,有一个 session 中(非持久)解决方案 - 假设命令是文化感知的并带有本地化字符串:
  • 设置 [cultureinfo]::CurrentUICulture (暂时)到所需的文化名称(使用 [cultureinfo]::GetCultures('SpecificCultures') 查看所有预定义的名称);例如,[cultureinfo]::CurrentUICulture = 'en-US'
  • 作为补充,您可能需要设置 [cultureinfo]::CurrentCulture (请注意缺少的 UI 部分),它决定了特定于文化的数字、日期……格式。
  • 在旧版本的 PowerShell/.NET 中,您必须在 [System.Threading.Thread]::CurrentThread 上设置这些属性。反而;例如。,[System.Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'

  • 底部对于 辅助函数 Use-Culture 包装此功能以执行具有不同文化的代码,暂时生效;这是一个示例调用
    与文化敏感 Get-LocalGroupMember 小命令:
    # 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 } 
    
  • 一个 特设示例 , 如果你不想定义辅助函数(这里只改变了 UI 文化):
    & {
      $prev=[cultureinfo]::CurrentUICulture
      [cultureinfo]::CurrentUICulture='en-US'
      Get-LocalGroupMember Administrators
      [cultureinfo]::CurrentUICulture=$prev
    }
    


  • 注意事项 :
  • PowerShell [Core] 本身尚未本地化,截至 v7.2.x ; GitHub issue #666 中正在跟踪进度;但是,下面的解决方案确实适用于带有本地化消息和帮助内容的第三方模块,以及选择与平台 API 对话的特定于 Windows 的模块,例如 Microsoft.PowerShell.LocalAccounts 模块,其 Get-LocalGroupMember上面的示例中使用了 cmdlet。
  • 由于 Windows PowerShell 中的错误 (PowerShell [Core] v6+ 不受影响), session 中更改为 [cultureinfo]::CurrentUICulture[cultureinfo]::CurrentCulture在命令提示符处自动重置,只要命令执行完毕 ;但是,对于给定的脚本,更改对整个脚本及其被调用者仍然有效 - 参见 this answer .

  • 退后一步:

    I wrote some software that uses the output of system (powershell) commands, but did not foresee that the output would be different for languages other than English.


    这正是 的原因通常值得寻找 PowerShell 原生解决方案,而不是调用外部程序 :
    不必像 netstat.exe 那样解析——可能是本地化的——文本。 ,例如,PowerShell 命令返回对象,您可以以与文化无关的方式可靠地访问其属性。
    具体来说,Mathias R. Jessen建议查看 Get-NetTCPConnection 作为 netstat.exe 的 PowerShell 替代品(适用于 Windows Server 2012/Windows 8 及更高版本)。

    功能 Use-Culture的源代码:
    注意:代码非常感谢地改编自 this venerable blog post ;它是设计的
    # 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]
      }
    }
    

    关于powershell - 暂时将powershell语言更改为英语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59909992/

    相关文章:

    powershell - 批量编写和执行Powershell脚本

    powershell - 串联Powershell命令

    用于按计划自动缩放 Function App 的 Azure 自动化和逻辑应用

    memory - PowerShell foreach 文件在文件夹中使用大量内存

    windows - 从 .exe 中获取签名者姓名的命令

    powershell - 是否可以在不使用临时文件的情况下添加列并删除双引号?

    git - Powershell 在加载 ssh-agent/git 时需要很长时间才能启动

    api - 使用 Servicenow Rest API 和 Powershell 查询多个表

    excel - export-csv powershell后格式化excel文件

    windows - 在powershell中合并2个csv文件