发布于 2020-01-25 16:47:19
E 115
_<代码>E 216会话中的**_:- 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**.
- 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.
E 232E 133
是E 234e 135
会话内(非持久性)解决方案e 236
-假设这些命令是区域性的,并且带有本地化的字符串。- 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'`
- 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'
- 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管理员}
- 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 }
警告
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的源代码:
注意:代码是从
这篇受人尊敬的博文
中修改而来的;它是设计出来的
# 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]
}
}
发布于 2020-01-25 15:30:09
这段代码的原作者是@Scepticalist。
从powershell控制台运行此命令。它将将当前会话的文化更改为en-US。
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
https://stackoverflow.com/questions/59909992
复制相似问题