Powershell以字符串形式写入主机输出变量中的参数 $mycolorparams=“-foregroundcolor红色-backgroundcolor黑色” 写主机“我希望这个前景是红色的,背景是黑色的”$mycolorparams

Powershell以字符串形式写入主机输出变量中的参数 $mycolorparams=“-foregroundcolor红色-backgroundcolor黑色” 写主机“我希望这个前景是红色的,背景是黑色的”$mycolorparams,powershell,Powershell,大家好 这让我快发疯了。使用write host时,cmdlet将以字符串形式返回所有内容: “我希望这个前景是红色的,背景是黑色的-前景是红色的-背景是黑色的” 不是红色文本和黑色背景的实际字符串 最糟糕的是,在我更改代码中的var名称之前,这对我来说是有效的。我不知道从那以后发生了什么变化。我怀疑这与引号有关,因为单引号吐出字符串,双引号读取变量。但是在文本和变量上尝试了大量单引号和双引号的变体后,结果是相同的,只是一个字符串输出 在过去的一个小时里,我一直在网上搜寻,运气不佳,有很多例子,

大家好

这让我快发疯了。使用write host时,cmdlet将以字符串形式返回所有内容:

“我希望这个前景是红色的,背景是黑色的-前景是红色的-背景是黑色的”

不是红色文本和黑色背景的实际字符串

最糟糕的是,在我更改代码中的var名称之前,这对我来说是有效的。我不知道从那以后发生了什么变化。我怀疑这与引号有关,因为单引号吐出字符串,双引号读取变量。但是在文本和变量上尝试了大量单引号和双引号的变体后,结果是相同的,只是一个字符串输出


在过去的一个小时里,我一直在网上搜寻,运气不佳,有很多例子,但我找不到关于具体问题的任何东西。谢谢你的帮助

嗯。。我为您提供了一个包装函数形式的小解决方案:

$mycolorparams = " -foregroundcolor red -backgroundcolor black"
function redBlack($text){
    invoke-expression ("write-host " + $text + $mycolorparams)
}
然后执行

redBlack "I want this foreground in Red and background in black"

这将产生正确着色的结果。

嗯。。我为您提供了一个包装函数形式的小解决方案:

$mycolorparams = " -foregroundcolor red -backgroundcolor black"
function redBlack($text){
    invoke-expression ("write-host " + $text + $mycolorparams)
}
然后执行

redBlack "I want this foreground in Red and background in black"
这将产生正确着色的结果。

使用参数飞溅(尽管我认为它不在旧版本中,所以您可能需要升级到Powershell版本3或更高版本)

或:

您需要将选项字符串转换为哈希表或数组。事实上,如果您运行
help about\u Splatting
,您会发现其中一个示例正好涵盖了您的问题:

此示例演示如何在不同的环境中重用飞溅的值 命令。 本例中的命令使用Write-Host cmdlet写入消息 到主机程序控制台。它使用散点来指定前景 和背景色

To change the colors of all commands, just change the value of the $Colors
variable.

The first command creates a hash table of parameter names and values and 
stores the hash table in the $Colors variable.

           $Colors = @{ForegroundColor = "black"
                       BackgroundColor = "white"}

The second and third commands use the $Colors variable for splatting in a
Write-Host command. To use the $Colors variable, replace the dollar sign 
($Colors) with an At symbol (@Colors).

           # Write a message with the colors in $Colors
           Write-Host "This is a test." @Colors

           # Write second message with same colors. 
           # The position of splatted hash table does not matter.
           Write-Host @Colors "This is another test."
使用参数散点(虽然我认为它不在旧版本中,所以您可能需要升级到Powershell版本3或更高版本)

或:

您需要将选项字符串转换为哈希表或数组。事实上,如果您运行
help about\u Splatting
,您会发现其中一个示例正好涵盖了您的问题:

此示例演示如何在不同的环境中重用飞溅的值 命令。 本例中的命令使用Write-Host cmdlet写入消息 到主机程序控制台。它使用散点来指定前景 和背景色

To change the colors of all commands, just change the value of the $Colors
variable.

The first command creates a hash table of parameter names and values and 
stores the hash table in the $Colors variable.

           $Colors = @{ForegroundColor = "black"
                       BackgroundColor = "white"}

The second and third commands use the $Colors variable for splatting in a
Write-Host command. To use the $Colors variable, replace the dollar sign 
($Colors) with an At symbol (@Colors).

           # Write a message with the colors in $Colors
           Write-Host "This is a test." @Colors

           # Write second message with same colors. 
           # The position of splatted hash table does not matter.
           Write-Host @Colors "This is another test."

我不得不添加一点额外的代码,因为它连接-foregroundcolor。但是谢谢你,它是有效的:)调用表达式(“write host”+$text+“”+$whBad)听起来很危险,如果$text包含相当于windows的
;rm-rf/
我不得不添加一点额外的代码,因为它连接了-foregroundcolor。但是谢谢你,它是有效的:)调用表达式(“write host”+$text+“”+$whBad)听起来很危险,如果$text包含相当于windows的
;rm-rf/
谢谢邓肯。语法让我想起了PHP,但我对它有点生疏了。为你的投入干杯,不过,我从你发布的示例中学到了一些东西1:)谢谢邓肯。语法让我想起了PHP,但我对它有点生疏了。为你的投入干杯,不过,我从你发布的示例中学到了一些东西1 :)