在我的一个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
}