具有多种颜色的读主机

时间:2018-11-19 22:00:43

标签: powershell

在我的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,否则我将在其中放置指示(我知道如何用多种颜色来完成 )。

1 个答案:

答案 0 :(得分:1)

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

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
}