【问题标题】:How to make Powershell ISE support colors and non-ASCII characters?如何使 Powershell ISE 支持颜色和非 ASCII 字符?
【发布时间】:2015-08-25 02:11:21
【问题描述】:

以下是我在项目目录中运行某个 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 的包装,但肯定还有更多。

更新: 遇到了this post,上面写着“控制台应用程序输出不是彩色的”。这是否意味着无法通过 PowerShell ISE 获得类似 PowerShell 的输出?

【问题讨论】:

    标签: powershell powershell-ise


    【解决方案1】:

    我会质疑您为什么希望它在 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 调用,这将导致问题。
    • 试试看。在 ISE 中运行时,对 Read-Host 的调用通常会创建一个提示窗口,您可以在其中输入(因为您无法在 ISE 的输出窗口中输入),但如果您从单独的 PowerShell 进程调用它,它不会显示你从脚本开始。看我的回答here
    • @jpmc26 我试过了;它似乎工作得很好。 Read-Host 在控制台主机中工作;它只是不会弹出一个新窗口,它可以让您直接输入。阅读您的答案后,您似乎正在 ISE 中运行 powershell.exe(这意味着它正在共享 ISE 的“控制台”窗口)。在我的方法中,我特别使用Start-Process,因为它会在自己的控制台主机中打开一个全新的powershell.exe 实例。
    【解决方案2】:

    出于好奇,您使用的是 git-bash 之类的东西吗?这些颜色字符对我来说似乎是 Bash 颜色字符,并且 ISE 使用 cmd.exe 作为其主机(我相信)。

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多