更改powershell变量的背景/前景色

时间:2015-03-04 12:47:33

标签: powershell

所以我有一个powershell脚本来检查IP是否可用,最后它会计算可用数量和不可用数量。

我是否可以更改$ used&的背景/前景色。 $ avail变量?

Clear-Host
$Ping = New-Object System.Net.Networkinformation.ping
$IP = Read-Host 'What ip range you want to scan (eg. 192.168.0.)?'
$pused = 0
$pavail = 0
for ($i=1; $i -le 254; $i++)
{
                $Status = $Null
                $IPAddress = $IP + $i
                $Status = ($Ping.Send("$IPAddress", 1)).Status

                if ($Status -eq "TimedOut")
                {
                Write-Host "$IPAddress is available!" -BackgroundColor Green
                $avail = $pavail++
                } else {
                Write-Host "$IPAddress is in USE!" -BackgroundColor Red
                $used = $pused++
                }
}
Write-Host ""
Write-Host $used  -BackgroundColor Red "IPs currently in use" -foregroundcolor "black"
Write-Host ""
Write-Host $avail "IPs currently available" -BackgroundColor Green -foregroundcolor "black"

1 个答案:

答案 0 :(得分:1)

我能想到做你想做的事的唯一方法是使用多个Write-Host行。

Write-Host ""
Write-Host -NoNewline $pused -ForegroundColor Black -BackgroundColor Red
Write-Host " IPs currently in use" 
Write-Host ""
Write-Host -NoNewline $pavail -ForegroundColor Black -BackgroundColor Green
Write-Host " IPs currently available"

您可以使用-NoNewline参数,以便输出仍将打印到控制台中的同一行。