我需要根据Test-Connection
-quiet cmdlet的输出来更改文本的颜色。
如果为真绿色,则为False红色。
我尝试使用写主机,但是没有运气
$StartIP = Read-Host -Prompt 'Input Start IP'
$EndIP = Read-Host -Prompt 'Input End IP'
$results= ([int]$StartIP..[int]$EndIP) | % {"192.168.128.$($): $(Test-Connection -count 1 -comp 192.168.128.$($) -quiet)"}
Read-Host -Prompt "Press Enter to exit"
答案 0 :(得分:2)
遵循现有代码:
$start = (Read-Host -Prompt Start) -as [int]
$end = (Read-Host -Prompt End) -as [int]
$start..$end | ForEach-Object {
$ip = "192.168.128.$_"
if (Test-Connection -Count 1 -Quiet -ComputerName $ip) {
"$ip TRUE" | Write-Host -ForegroundColor Green
}
else {
"$ip FALSE" | Write-Host -ForegroundColor Red
}
}