【问题标题】:Change the current culture of a Powershell session, v3.0+ specific更改 Powershell 会话的当前文化,特定于 v3.0+
【发布时间】:2015-06-19 11:27:47
【问题描述】:

我想改变当前交互式 Powershell 会话中接受数据和错误的文化。我知道这个问题powershell : changing the culture of current session 和这个问题Changing current culture 关于超级用户。它不适用于 Powershell 3.0 和 4.0 的主要问题。

PS C:\users\me\Documents> [system.threading.thread]::currentthread.currentculture

LCID             Name             DisplayName
----             ----             -----------
1049             ru-RU            Русский (Россия)


PS C:\users\me\Documents> [system.threading.thread]::currentthread.currentculture=[system.globalization.cultureinfo]"en-US"
PS C:\users\me\Documents> [system.threading.thread]::currentthread.currentculture

LCID             Name             DisplayName
----             ----             -----------
1049             ru-RU            Русский (Россия)

UI 文化也不接受新设置。 Set-Culture 总共不起作用,无论我是否使用管理员访问权限 - 无论如何,它不应该受此影响,因为效果仅适用于单个进程。来自 MSDN Powershell 博客的 Using-Culture 由 SO 社区改编,但仅部分有效,例如,在当前的“ru-RU”文化下,我能够从“6/19/15 2:26: 02 PM" 字符串,通过Using-Culture "en-US" {get-date -date "6/19/15 2:26:02 PM"} 在“en-US”文化中,但不可能收到另一种语言的错误:比如Using-Culture "en-US" {$null.test='1'} 导致俄语语言环境出错,就好像文化没有改变.

此行为已在安装了 Powershell 4.0 的本地 Win7 Professional 工作站和安装了 Powershell 3.0 的 Windows Server 2012 上进行了测试,这是解析错误本地化的日期字符串所必需的。后者具有“en-US”的 UI 文化和系统区域设置“ru-RU”。

那么,PS3 及更高版本是否仍然可以改变 Powershell 会话的文化,如果可以,如何? (或者是再次出现错误,还是我不知道的 PS3 变化?)

【问题讨论】:

    标签: powershell powershell-3.0 culture powershell-4.0


    【解决方案1】:

    更改区域性仅影响线程并且仅适用于该进程。 您的 PS 窗口在当前语言环境下启动,因此线程具有该语言环境。在当前系统语言环境下启动的 PS 窗口中键入“[System.Threading.Thread]::CurrentThread.CurrentCulture”,将始终显示该语言环境。

    如果你在 ISE 中运行它,它应该很少解释:

     function Set-Culture([System.Globalization.CultureInfo] $culture) {
    [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture }
    
    Set-Culture en-US
    [system.threading.thread]::currentthread.currentculture
    Pause
    

    或者,在 PS 窗口中:

    function Set-Culture([System.Globalization.CultureInfo] $culture) { [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture ; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture } ; Set-Culture en-US ; [system.threading.thread]::currentthread.currentculture
    

    效果很好。

    如果您想要一个具有新文化的 PS 窗口,您需要使用该文化启动它,而不是在之后尝试更改它。

    【讨论】:

    • 要在文化下启动一个PSwindow,只需将以下内容添加到上述任一:start-process 'c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
    • 我试过这个没有运气(一直显示非英语输出),我做错了什么? Set-Culture en-US start-process 'c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' 在里面我运行netsh wlan show drivers
    • 你不能那样做。我可以进一步澄清一下,设置文化必须在当前进程中完成 - 要么在新进程窗口中运行命令作为脚本的一部分,要么你可以执行类似 Start-Job -scriptblock {function Set-Culture([System. Globalization.CultureInfo] $culture) { [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture ; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture } ; Set-Culture en-US ;等等.....}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多