Forcing PowerShell errors output in English on localized systems

后端 未结 2 1250
有刺的猬
有刺的猬 2020-11-27 21:51

I need to run some PowerShell scripts across various operating systems. Most of them are in English version, however, some are localized for example German, French, Spanish,

2条回答
  •  不知归路
    2020-11-27 21:53

    [Threading.Thread]::CurrentThread.CurrentUICulture only affects to current one-liner, so you can use it for execution of single .ps1 file.

    If you want to change messages to English throughout every command in a PowerShell window, you have to change the culture setting cached in PowerShell runtime with reflection like this:

    # example: Set-PowerShellUICulture -Name "en-US"
    
    function Set-PowerShellUICulture {
        param([Parameter(Mandatory=$true)]
              [string]$Name)
    
        process {
            $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture($Name)
    
            $assembly = [System.Reflection.Assembly]::Load("System.Management.Automation")
            $type = $assembly.GetType("Microsoft.PowerShell.NativeCultureResolver")
            $field = $type.GetField("m_uiCulture", [Reflection.BindingFlags]::NonPublic -bor [Reflection.BindingFlags]::Static)
            $field.SetValue($null, $culture)
        }
    }
    

    (from https://gist.github.com/sunnyone/7486486)

提交回复
热议问题