具有多种颜色的读取主机

bgm*_*der 2 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
Run Code Online (Sandbox Code Playgroud)

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

此外,-foregroundcolor在这里不起作用 - 它只是按字面打印。

我不能write-hostparam声明之前放一个,或者我会把方向放在那里(我知道如何用多种颜色来做到这一点)。

Dre*_*rew 5

你似乎对评论中的说明有点挣扎,所以在这里......不确定你为什么想要阅读主机的说明,但这很酷。

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
}
Run Code Online (Sandbox Code Playgroud)