gpt4 book ai didi

powershell - 读主机具有多种颜色

转载 作者:行者123 更新时间:2023-12-03 01:02:54 25 4
gpt4 key购买 nike

在我的Powershell函数之一中,我想从用户那里收集输入,但是首先我需要给出一些说明。我想在控制台中用不同的颜色打印一行或两行。

function myFunction(){
param(
[string]$directions = $(read-host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based." -foregroundcolor "Magenta"),
[string]$robot = $(read-host "What is your favourite robot" -foregroundcolor "Yellow"),
[string]$spaceship = $(read-host "What is your favourite spaceship" -foregroundcolor "Green")
)
write-host "Favourite Robot = " + $robot
write-host "Favourite Spaceship = " + $spaceship
}
#call the function
myFunction

在上面的函数中,我有一个换行符以将方向保持在单独的级别上,但是我希望此文本的第一行是一种颜色,第二行是另一种颜色。

另外, -foregroundcolor在这里不起作用-只是按字面意义打印。

我不能将 write-host放在 param语句之前,否则我将在其中放置说明(我知道如何使用多种颜色)。

最佳答案

您似乎对注释中的说明有些挣扎,所以在这里...不确定为什么要阅读主机作为说明,但这很酷。

function myFunction(){
param(
[string]$directions = $(Write-Host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based.: " -ForegroundColor Magenta -NoNewline; Read-Host),
[string]$robot = $(Write-Host "What is your favourite robot: " -ForegroundColor Yellow -NoNewline; Read-Host),
[string]$spaceship = $(Write-Host "What is your favourite spaceship: " -ForegroundColor Green -NoNewline; Read-Host)
)
write-host "Favourite Robot = "$robot
write-host "Favourite Spaceship = "$spaceship
}

关于powershell - 读主机具有多种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53383257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com