我编写了一些使用系统(powershell)命令输出的软件,但没有预见到英语以外的语言的输出会有所不同。
有没有办法将 Powershell 中的语言临时更改为英语,仅用于那个单一的 Powershell session ?
笔记
netstat -n -a
最佳答案
netstat.exe
,不幸的是(据我所知)无法在 session 中更改 UI 语言 :Set-WinUILanguageOverride
小命令 允许您(持续地)更改当前用户的系统范围的 UI 语言 , 但这只在以后的登录 session 中生效 - 即 需要注销并重新启动或重新启动 .Set-Culture
cmdlet,但它的 目的不是改变 UI 文化(显示语言) , 但仅限于特定文化 日期、数字和货币格式等设置 .它也会持续更改当前用户的设置,但只需要一个新的 session (进程)才能使更改生效。[cultureinfo]::CurrentUICulture
(暂时)到所需的文化名称(使用 [cultureinfo]::GetCultures('SpecificCultures')
查看所有预定义的名称);例如,[cultureinfo]::CurrentUICulture = 'en-US'
[cultureinfo]::CurrentCulture
(请注意缺少的 UI
部分),它决定了特定于文化的数字、日期……格式。 [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 }
& {
$prev=[cultureinfo]::CurrentUICulture
[cultureinfo]::CurrentUICulture='en-US'
Get-LocalGroupMember Administrators
[cultureinfo]::CurrentUICulture=$prev
}
注意事项 :
Microsoft.PowerShell.LocalAccounts
模块,其 Get-LocalGroupMember
上面的示例中使用了 cmdlet。[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/