我需要根据Test-Connection
-quiet cmdlet的输出更改文本的颜色。
如果为真绿色,如果为假红色。
我尝试使用写主机,但没有运气
$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"
按照现有代码:
$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
}
}