如何在同一行上连接 2 个 Write-Output?
How to concatenate 2 Write-Output on the same line?
我写了一个脚本,可以 return 你是否安装了软件。
我想给它一些颜色,但我不知道如何在同一行上连接 2 Write-Output
,因为 -NoNewline
仅适用于 Write-Host
.. . 在这种情况下我不能使用:
# The function we use to give color
function Positive {
process { Write-Host $_ -ForegroundColor Green }
}
function Negative {
process { Write-Host $_ -ForegroundColor Red }
}
# Is the software installed?
# '0' = NO, is not installed
# '1' = YES, is installed
$Check = '1'
function Check_Installation($Check){
if ($Check -eq '0') {return $response = "No, is not installed" | Negative}
elseif ($Check -eq '1') {return $response = "Yes, is installed" | Positive}
}
$First_Phrase = "Let's check if the software is installed: "
Write-Output "$First_Phrase", "$response"
Check_Installation($Check)
我知道我可以连接
[string]::Concat("$First_Phrase", "$response")
但不工作。
这仅在控制台中有效,因为在 ISE 中更改前景颜色会更改每一行:
# The function we use to give color
function Set-Output {
param ($colour, $str1, $str2)
$t = $host.ui.RawUI.ForegroundColor
$host.ui.RawUI.ForegroundColor = "$colour"
$text = "$str1 $str2"
Write-Output "$text"
$host.ui.RawUI.ForegroundColor = $t
}
# Is the software installed?
# '0' = NO, is not installed
# '1' = YES, is installed
$Check = '1'
$First_Phrase = "Let's check if the software is installed: "
Switch ($check) {
( 0 ) {Set-Output -colour RED -str1 $First_Phrase -str2 "No, is not installed" }
( 1 ) {Set-Output -colour GREEN -str1 $First_Phrase -str2 "Yes, is installed" }
}
Check_Installation($Check)
它所做的只是连接两个字符串并更改前景色。
我写了一个脚本,可以 return 你是否安装了软件。
我想给它一些颜色,但我不知道如何在同一行上连接 2 Write-Output
,因为 -NoNewline
仅适用于 Write-Host
.. . 在这种情况下我不能使用:
# The function we use to give color
function Positive {
process { Write-Host $_ -ForegroundColor Green }
}
function Negative {
process { Write-Host $_ -ForegroundColor Red }
}
# Is the software installed?
# '0' = NO, is not installed
# '1' = YES, is installed
$Check = '1'
function Check_Installation($Check){
if ($Check -eq '0') {return $response = "No, is not installed" | Negative}
elseif ($Check -eq '1') {return $response = "Yes, is installed" | Positive}
}
$First_Phrase = "Let's check if the software is installed: "
Write-Output "$First_Phrase", "$response"
Check_Installation($Check)
我知道我可以连接
[string]::Concat("$First_Phrase", "$response")
但不工作。
这仅在控制台中有效,因为在 ISE 中更改前景颜色会更改每一行:
# The function we use to give color
function Set-Output {
param ($colour, $str1, $str2)
$t = $host.ui.RawUI.ForegroundColor
$host.ui.RawUI.ForegroundColor = "$colour"
$text = "$str1 $str2"
Write-Output "$text"
$host.ui.RawUI.ForegroundColor = $t
}
# Is the software installed?
# '0' = NO, is not installed
# '1' = YES, is installed
$Check = '1'
$First_Phrase = "Let's check if the software is installed: "
Switch ($check) {
( 0 ) {Set-Output -colour RED -str1 $First_Phrase -str2 "No, is not installed" }
( 1 ) {Set-Output -colour GREEN -str1 $First_Phrase -str2 "Yes, is installed" }
}
Check_Installation($Check)
它所做的只是连接两个字符串并更改前景色。