首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >暂时将powershell语言改为英语?

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

Stack Overflow用户
提问于 2020-01-25 14:07:21
回答 2查看 13.7K关注 0票数 14

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

是否有一种方法可以暂时将Powershell中的语言更改为英语,只用于这一次,即单个powershell会话

备注

  • 如果有意义,我希望运行的特定powershell代码是netstat -n -a
  • 我遇到了一些改变powershell语言的方法(例如这里这里)。但我要小心,不要永远改变它!(那就不好了)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-25 16:47:19

  • (a) For 外部程序,例如不幸的是,_没有_<代码>E 214方式(据我所知)可以更改UI语言E 115_<代码>E 216会话中的**_:
代码语言:javascript
运行
复制
- On Windows Server 2012 / Windows 8 and above, the [**`Set-WinUILanguageOverride`**](https://learn.microsoft.com/en-us/powershell/module/international/set-winuilanguageoverride) **cmdlet** allows you to (persistently) **change the system-wide UI language for the current user**, but that only takes effect in _future_ logon sessions - that is, **logging off and back on or a reboot are required**.
代码语言:javascript
运行
复制
- As an aside: On Windows Server 2012 / Windows 8 and above, there is also the [**`Set-Culture`**](https://learn.microsoft.com/en-us/powershell/module/international/set-culture) cmdlet, but its **purpose is** _**not**_ **to change the** _**UI**_ **culture (display language)**, but only culture-specific **settings such as date, number, and currency formats**. It too changes the setting _persistently_ for the current user, but only requires a new _session_ (process) for the change to take effect.
  • (b) For PowerShell命令和 .NET types,其中E 232E 133E 234e 135会话内(非持久性)解决方案e 236-假设这些命令是区域性的,并且带有本地化的字符串。
代码语言:javascript
运行
复制
- Set [`[cultureinfo]::CurrentUICulture`](https://learn.microsoft.com/en-US/dotnet/api/System.Globalization.CultureInfo.CurrentUICulture) (temporarily) to the desired culture name (use [`[cultureinfo]::GetCultures('SpecificCultures')`](https://learn.microsoft.com/en-US/dotnet/api/system.globalization.cultureinfo.getcultures) to see all predefined ones) ; e.g.,  `[cultureinfo]::CurrentUICulture = 'en-US'`
代码语言:javascript
运行
复制
    - Complementarily, you may want to set [`[cultureinfo]::CurrentCulture`](https://learn.microsoft.com/en-US/dotnet/api/System.Globalization.CultureInfo.CurrentCulture) (note the missing `UI` part) as well, which determines the culture-specific number, date, ... formatting.
    - In older versions of PowerShell / .NET, you'll have to set these properties on [`[System.Threading.Thread]::CurrentThread`](https://learn.microsoft.com/en-US/dotnet/api/System.Threading.Thread.CurrentThread) instead; e.g.,

[System.Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'

代码语言:javascript
运行
复制
- See the **bottom section** for **helper function** **`Use-Culture`** that wraps this functionality for execution of code with a different culture temporarily in effect; here's an example call with the culture-sensitive [`Get-LocalGroupMember`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localgroupmember) cmdlet:

尝试使用"en-US“以外的值,例如"fr-FR”,以查看"ObjectClass“输出列中的本地化#值。使用-区域性en-US { Get-LocalGroupMember管理员}

代码语言:javascript
运行
复制
- An **ad hoc example**, if you don't want to define a helper function (only the UI culture is changed here):

&{ $prev=cultureinfo::CurrentUICulture cultureinfo::CurrentUICulture=‘en’Get-LocalGroupMember cultureinfo::CurrentUICulture=$prev }

警告

  • PowerShell Core 本身是而不是本地化的,截至v7.2.x;但是,下面的解决方案确实适用于带有本地化消息和帮助内容的第三方模块,以及选择与平台API对话的特定于Windows的模块,如Microsoft.PowerShell.LocalAccounts模块,其Get-LocalGroupMember cmdlet在上面的示例中使用。
  • 由于[cultureinfo]::CurrentUICulture Windows PowerShell中的错误(PowerShell Core v6+不受影响),每当命令提示符执行完E 282时,对Windows和 [cultureinfo]::CurrentCulture 的会话中的更改为E 278E 179自动重置E 280<>E 181;然而,对于给定的脚本,更改对整个脚本及其调用仍然有效--参见C 83。

后退一步:

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

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

例如,不必解析(可能是本地化的) text,netstat.exe**,,PowerShell命令返回_对象_,您可以以与区域性无关的方式可靠地访问它的属性。**

具体来说,马蒂亚斯·耶森建议将Get-NetTCPConnection视为netstat.exe的一种PowerShell替代方案(可在Windows 2012 /Windows8及更高版本上使用)。

函数Use-Culture的源代码:

注意:代码是从这篇受人尊敬的博文中修改而来的;它是设计出来的

代码语言:javascript
运行
复制
# 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]
  }
}
票数 11
EN

Stack Overflow用户

发布于 2020-01-25 15:30:09

这段代码的原作者是@Scepticalist。

从powershell控制台运行此命令。它将将当前会话的文化更改为en-US。

代码语言:javascript
运行
复制
function Set-CultureWin([System.Globalization.CultureInfo] $culture) { [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture ; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture } ; Set-CultureWin en-US ; [system.threading.thread]::currentthread.currentculture

然后,您必须使用命令Get-NetTCPConnection而不是netstat。有关其使用情况,请参阅https://learn.microsoft.com/en-us/powershell/module/nettcpip/get-nettcpconnection?view=win10-ps

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59909992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档