我需要跨各种操作系统运行一些PowerShell脚本.其中大多数都是英文版,但有些是本地化的,例如德语,法语,西班牙语等.问题是本地系统管理员现在大部分时间都没有PowerShell,如果脚本失败并向他们抛出错误,而不是读它,他们只是发送这样的错误信息的截图给我,如果这个错误的原因不明显,我很难打字到g.翻译以找出正在发生的事情.
是否有一个开关我可以运行整个脚本或单个命令或参数或任何其他方式强制PowerShell中的错误以英语显示而不是该特定机器的默认语言?
我编写了一些使用系统(powershell)命令输出的软件,但没有预见到除英语之外的其他语言的输出会有所不同。
有没有办法暂时将 Powershell 中的语言更改为英语,仅用于单个 powershell 会话?
我想测试一下我的应用程序在不同文化下的工作方式.因此,出于测试目的,在Windows 7下,我尝试更改CurrentUICulture系统设置.
这似乎是正确的选择:Language for non-Unicode programs 如此处所示,但它不起作用,即应用程序的语言环境仍然是英语.
我也在Region and Language对话框中试过这个:
问题是我应该在Windows 7中设置什么才能影响:
Thread.CurrentThread.CurrentUICulture
Run Code Online (Sandbox Code Playgroud)
而不是写这个:
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr")
Run Code Online (Sandbox Code Playgroud)
最终,这段代码应该选择正确的文化,获取正确的后缀资源文件并将其显示在屏幕上(它与上面的行一样):
Label1.Text = My.Resources.Form1Resource.TestString
Run Code Online (Sandbox Code Playgroud)
StackOverflow也提出了类似的问题,但没有一个答案解决了这个问题.
修改 PowerShell 脚本中的某些方面似乎会修改正在运行的 shell。如果我提供脚本而不是运行它,这将是意料之中的。
脚本.ps1
[cultureinfo]::currentculture = [cultureinfo]::InvariantCulture
Set-PSDebug -Trace 1
Run Code Online (Sandbox Code Playgroud)
> [cultureinfo]::currentculture
LCID Name DisplayName
---- ---- -----------
1031 de-DE Deutsch (Deutschland)
> .\script.ps1
> [cultureinfo]::currentculture
DEBUG: 1+ >>>> [cultureinfo]::currentculture
LCID Name DisplayName
---- ---- -----------
127 Unveränderliche Sprache (Unveränderliches Land bzw. unveränderliche Region)
Run Code Online (Sandbox Code Playgroud)
很明显,调试跟踪是活跃的,并且文化变化持续存在......