如何使Powershell ISE支持颜色和非ASCII字符?

4

当我在我的项目目录中运行某个grunt任务时,以下是来自Windows PowerShell的一些示例输出:

Done, without errors.


Execution Time (2015-08-25 01:57:14 UTC)
loading tasks                9ms  ██████ 17%
loading grunt-contrib-copy  23ms  ███████████████ 43%
copy:styles                 20ms  █████████████ 38%
Total 53ms

Running "autoprefixer:server" (autoprefixer) task
Autoprefixer's process() method is deprecated and will removed in next major release. Use postcss([autoprefixe
r]).process() instead
File .tmp/styles/main.css created.

“完成,无错误”显示为绿色,“.tmp/styles/main.css”显示为青色。在Powershell ISE中运行相同的命令,会产生以下结果:
[32mDone, without errors.[39m


Execution Time (2015-08-25 01:58:40 UTC)
loading tasks                9ms  ███████ 20%
loading grunt-contrib-copy  20ms  ████████████████ 45%
copy:styles                 14ms  ███████████ 32%
Total 44ms

[4mRunning "autoprefixer:server" (autoprefixer) task[24m
grunt : Autoprefixer's process() method is deprecated and will removed in next major release. Use 
postcss([autoprefixer]).process() instead
At line:1 char:1
+ grunt serve
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Autoprefixer's ...ocess() instead:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

File .tmp/styles/main.css created.

颜色和非ASCII字符都没有被正确地解释。

我尝试过搜索解决方案,但没有成功。如何使PowerShell ISE的行为更像PowerShell?听起来PS ISE只是常规 PS 的包装器,但肯定还有更多内容。

更新:发现这篇文章说“控制台应用程序输出不是彩色的”。这是否意味着无法通过PowerShell ISE获得类似于PowerShell的输出?

2个回答

0

我想问一下,为什么你想让这个在PowerShell ISE中工作?只是为了在开发/调试时看到正确的输出,而正常执行是在powershell.exe中完成吗?

如果是这样,你可以通过在脚本顶部添加以下内容来解决:

if ($Host.Name -like '*ISE*') {
    Start-Process -FilePath powershell.exe -ArgumentList '-NoExit','-ExecutionPolicy',(Get-ExecutionPolicy),'-File',$PSCommandPath
    return
}

基本上,每当你在ISE中运行脚本时,它将在一个新的PowerShell控制台主机中运行脚本。

如果脚本中有任何 Read-Host 调用,这将会引起问题。 - jpmc26
试一试看看。当在ISE中运行时,对Read-Host的调用通常会创建一个提示窗口,在该窗口中您可以输入(因为您无法在ISE的输出窗口上输入),但是如果您从脚本中启动了单独的PowerShell进程,则不会出现该窗口。请参见我的回答在这里 - jpmc26
@jpmc26 我已经尝试过了,它似乎完美地运行。Read-Host在控制台主机中工作;它只是不会弹出新窗口,而是让你直接输入。在阅读了你的答案之后,看起来你正在ISE中运行powershell.exe(这意味着它正在共享ISE的“控制台”窗口)。在我的方法中,我使用Start-Process特别是因为它打开一个全新的powershell.exe实例,在它自己的控制台主机中。 - briantist

0

出于好奇,你是在使用类似于git-bash的东西吗?那些颜色字符对我来说看起来像Bash颜色字符,而ISE使用cmd.exe作为其主机(我相信)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接