Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如果Windows PowerShell的颜色在运行程序后损坏,您如何重置这些颜色?_Node.js_Powershell_Colors - Fatal编程技术网

Node.js 如果Windows PowerShell的颜色在运行程序后损坏,您如何重置这些颜色?

Node.js 如果Windows PowerShell的颜色在运行程序后损坏,您如何重置这些颜色?,node.js,powershell,colors,Node.js,Powershell,Colors,我开始使用node.js为Windows项目使用PowerShell。从Powershell命令行运行许多程序(包括节点、主管和npm)时,我的Powershell背景和前景颜色开始从默认Powershell颜色更改。如何在PowerShell中保持一致的外观,以便轻松读取运行命令的结果 如果您还没有配置文件,请首先在PowerShell中创建配置文件: test-path $profile new-item -path $profile -itemtype file -force notepa

我开始使用node.js为Windows项目使用PowerShell。从Powershell命令行运行许多程序(包括节点、主管和npm)时,我的Powershell背景和前景颜色开始从默认Powershell颜色更改。如何在PowerShell中保持一致的外观,以便轻松读取运行命令的结果

如果您还没有配置文件,请首先在PowerShell中创建配置文件:

test-path $profile
new-item -path $profile -itemtype file -force
notepad $profile
其次,将此代码放入文件中:

function prompt {
  [Console]::ResetColor()
}
第三,检查PowerShell是否允许您运行脚本

Get-ExecutionPolicy
如果这表示受限,则以管理员身份运行以下操作(请确保您理解安全含义):


打开一个新的PowerShell提示符,您应该能够运行node或其他命令,而不会出现任何颜色问题。

我对MSBuild有此问题,尤其是当我按住ctrl+C组合键进行生成时。这是我在profile.ps1文件中输入的内容:

$OrigBgColor = $host.ui.rawui.BackgroundColor
$OrigFgColor = $host.ui.rawui.ForegroundColor

# MSBUILD has a nasty habit of leaving the foreground color red
# if you Ctrl+C while it is outputting errors.
function Reset-Colors {
    $host.ui.rawui.BackgroundColor = $OrigBgColor
    $host.ui.rawui.ForegroundColor = $OrigFgColor
}

然后,当MSBuild将颜色搞乱时,我只调用重置颜色。

作为一次性操作,只需运行以下操作:

> [Console]::ResetColor()
(增加重点)

前景和背景色恢复为当前进程开始时存在的颜色


我通过运行以下命令修复了颜色问题:

cmd
color 07
exit

这些命令的作用是在PowerShell中运行cmd,运行
color 07
修复颜色,然后退出cmd恢复到PowerShell。

注释添加,如果使用posh git,则可能使用默认配置文件,该配置文件定义了
提示符
函数,因此请确保添加
[控制台]::ResetColor()
到该函数,否则您将覆盖它,并失去所有漂亮的git功能。这应该是公认的答案。无需配置文件设置。这将是一个很好的公认答案,除了它不适用于我之外,实际上,在通过从另一个窗口复制
$host.ui.rawui
值手动修复问题后,这会使问题返回。哇,3个很好的答案。它不会重置颜色,而是将颜色设置为黑色上的白色。此外,它可能只是:“cmd/c color 07”
cmd
color 07
exit