.net 如何在 PowerShell 中设置文化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4105224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:54:17  来源:igfitidea点击:

How to set culture in PowerShell?

.netpowershellinternationalization

提问by Jader Dias

Is there any PowerShell equivalent for:

是否有任何 PowerShell 等效项:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

? Or how to set the force all stringfications to obey a culture independently from the machine configurations?

? 或者如何设置强制所有字符串化以遵守独立于机器配置的文化?

采纳答案by Jader Dias

I think this will work:

我认为这会奏效:

$currentThread = [System.Threading.Thread]::CurrentThread
$culture = [System.Globalization.CultureInfo]::InvariantCulture
$currentThread.CurrentCulture = $culture
$currentThread.CurrentUICulture = $culture

This idea came from:

这个想法来自:

http://blogs.msdn.com/b/powershell/archive/2006/04/25/583235.aspx

http://blogs.msdn.com/b/powershell/archive/2006/04/25/583235.aspx

回答by Keith Hill

This is the function I use for testing string/formats in other cultures:

这是我用于测试其他文化中的字符串/格式的函数:

function Using-Culture (
  [System.Globalization.CultureInfo]
  $culture = (throw "USAGE: Using-Culture -Culture culture -Script {...}"),
  [ScriptBlock]
  $script = (throw "USAGE: Using-Culture -Culture culture -Script {...}"))
{
    $OldCulture = [Threading.Thread]::CurrentThread.CurrentCulture
    $OldUICulture = [Threading.Thread]::CurrentThread.CurrentUICulture    
    try {        
        [Threading.Thread]::CurrentThread.CurrentCulture = $culture        
        [Threading.Thread]::CurrentThread.CurrentUICulture = $culture        
        Invoke-Command $script    
    }    
    finally {        
        [Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture        
        [Threading.Thread]::CurrentThread.CurrentUICulture = $OldUICulture    
    }
}

回答by mbx

For WinServer2012 and Win8 you can use Set-Culture. As Set-Culturesets the culture for your user, you'd have to open another powershell instance to benefit from that. Also it does not change the culture of running ps instances. Of course you could start up a new powershell instance in your current instance then. This is not exactly what is asked for, but closely related.

对于 WinServer2012 和 Win8,您可以使用Set-Culture. 作为Set-Culture集文化对你的用户,你必须从打开另一个PowerShell的实例好处。它也不会改变运行 ps 实例的文化。当然,您可以在当前实例中启动一个新的 powershell 实例。这并不完全是所要求的,而是密切相关的。

Would be neat, if they'd port that back to Win7 and Server 2008 or make it a feature of powershell itselft.

如果他们将其移植回 Win7 和 Server 2008 或使其成为 powershell 本身的功能,那就太好了。

回答by mklement0

This answer deals with the current culture, which determines settings such as date format, currency, number formatting, collating sequence, ...; by contrast, the current UI culture, determines the UI language (menus, error messages, ...); All elements discussed below have UI-culture analogs (e.g., Get-UICulturevs. Get-Culture, $PSUICulturevs. $PSCultureEXCEPT Set-Culture, for which there is no analog.

该答案涉及当前文化,它决定了日期格式、货币、数字格式、整理顺序等设置;相比之下,当前的 UI 文化,决定了 UI 语言(菜单、错误消息……);下面讨论的所有元素都有 UI 文化类似物(例如,Get-UICulturevs. Get-Culture$PSUICulturevs. $PSCultureEXCEPT Set-Culture,没有类似物。

Changing to a different culture:

转变为不同的文化

In the .NET Framework v4.6and higher, you can now assignto [cultureinfo]::CurrentCulture(previously, it was read-only[1]; the [cultureinfo]PS type accelerator was introduced in PSv3); e.g.:

.NET Framework v4.6及更高版本中,您现在可以分配[cultureinfo]::CurrentCulture(以前,它是只读的[1][cultureinfo]PS 类型加速器是在 PSv3 中引入的);例如:

[cultureinfo]::CurrentCulture = 'de-DE'

is equivalent to (which also works in v4.5 or lower, down to at least v2):

相当于(也适用于 v4.5 或更低版本,至少适用于 v2):

[System.Threading.Thread]::CurrentThread.CurrentCulture = 'de-DE'

CAVEAT: PowerShell uses the invariantculture in string-relatedcontexts, irrespective of what the current culture is- see this answerof mine.

CAVEAT:PowerShell在与字符串相关的上下文中使用不变文化,而不管当前的文化是什么- 请参阅我的这个答案

  • Both methods change the culture for the currentPowerShell instance (thread) only.

    • Caveat[fixed in PowerShell Coreas of at least v6.0.2]: As has been noted, in order to try this in an interactivePowerShell session, enter all commands on a singleline, because the culture-changing effect is limited to a single command line (this still applies on PSv3+, even though consoles there run in STA mode by default); e.g., to print a German date:
      [cultureinfo]::CurrentCulture = 'de-DE'; Get-Date # must be on same line
  • For a persistentculture change for the current user, use the Set-Culturecmdlet, but, as noted in mbx's helpful answer, this change only takes effect for futurePowerShell instances, NOT the current one.

  • 这两种方法都改变文化为当前的PowerShell实例(线程)

    • 买者[固定在PowerShell中核心至少V6.0.2的]:如已经指出的那样,为了尽量这在交互式PowerShell会话中,输入上的所有命令单个线,这是因为培养变化的效果仅限于单个命令行(这仍然适用于 PSv3+,即使那里的控制台默认以 STA 模式运行);例如,打印德国日期:
      [cultureinfo]::CurrentCulture = 'de-DE'; Get-Date # must be on same line
  • 对于当前用户的持续文化更改,请使用Set-Culturecmdlet,但是,正如mbx 的有用答案中所述,此更改仅对未来的PowerShell 实例生效,而不是当前的实例。



Querying culture settings:

查询文化设置:

  • [cultureinfo]::CurrentCultureand [System.Threading.Thread]::CurrentThread.CurrentCulturereflect the currentPowerShell instance's effectiveculture.

  • By contrast, the Get-Culturecmdlet(PSv3+) and the automatic $PSCulturevariable (PSv3+; read-only) invariably reflect the current PowerShell instance's culture at startuptime; i.e., they always reflect the current user's persistently configured culture at the time the current PowerShell instance was started(irrespective of instance-only changes via [cultureinfo]::CurrentCulture = ...or futurepersistent changes via Set-Cultureperformed in that instance).

  • [cultureinfo]::CurrentCulture[System.Threading.Thread]::CurrentThread.CurrentCulture反映当前PowerShell 实例的有效文化

  • 相比之下,Get-Culturecmdlet(PSv3+) 和自动$PSCulture变量(PSv3+;只读)在启动时总是反映当前 PowerShell 实例的文化;即,它们始终反映当前用户在当前 PowerShell 实例启动时持久配置的文化(无论通过该实例执行的仅实例更改[cultureinfo]::CurrentCulture = ...未来的持久更改如何Set-Culture)。



[1] See the docs; to determine whether you have at least v4.6 installed, look for the Version:value in the output from Get-Item 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'.
Note that the frameworkversion is distinct from the CLR(runtime) version as reported by $PSVersionTable.CLRVersion; for instance, the v4.6 frameworkis based on the v4.0 CLR- see the docs.

[1] 见文档;要确定您是否至少安装了 v4.6,请Version:Get-Item 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'.
请注意,框架版本与;报告的CLR(运行时)版本不同$PSVersionTable.CLRVersion。例如,v4.6框架基于 v4.0 CLR- 请参阅文档

回答by Steve Townsend

See herefor details of ObjectCmdletBase.CultureProperty.

请参阅此处了解ObjectCmdletBase.Culture属性的详细信息。

Gets and sets the value of the Culture parameter of the derived cmdlet.

获取和设置派生 cmdlet 的 Culture 参数的值。

回答by Ignore

Set-Cultureworks on local server but it does not work when you run it on remote computer

Set-Culture可在本地服务器上运行,但在远程计算机上运行时不起作用

Invoke-Command -ComputerName $server -ScriptBlock{
            #Requires -RunAsAdministrator
            Set-Culture -CultureInfo en-GB; 
            Set-TimeZone -Id "GMT Standard Time" 
        }