有没有办法获取服务器列表,执行“ForEach”命令来 ping 并响应控制台窗口,在 $x 列中显示所有服务器,覆盖它们的服务器名?如果服务器启动,我想将他们的名字显示为绿色,如果服务器关闭,则更改为红色。
到目前为止我得到了什么:
$ServerL = "filename.txt"
$ScreenD = "ScreenDisplay.ps1"
while ($true) {
if(Test-Path $ScreenD) {
Remove-item $ScreenD
}
function GetStatusCode {
param([int] $StatusCode)
switch($StatusCode) {
0 {"Success"}
11001{"Buffer Too Small"}
11002{"Destination Net Unreachable"}
11003 {"Destination Host Unreachable"}
11004 {"Destination Protocol Unreachable"}
11005 {"Destination Port Unreachable"}
11006 {"No Resources"}
11007 {"Bad Option"}
11008 {"Hardware Error"}
11009 {"Packet Too Big"}
11010 {"Request Timed Out"}
11011 {"Bad Request"}
11012 {"Bad Route"}
11013 {"TimeToLive Expired Transit"}
11014 {"TimeToLive Expired Reassembly"}
11015 {"Parameter Problem"}
11016 {"Source Quench"}
11017 {"Option Too Big"}
11018 {"Bad Destination"}
11032 {"Negotiating IPSEC"}
11050 {"General Failure"}
default {"Failed"}
}
}
Write-Host "`n"
foreach($Server In Gc $ServerList) {
$PingStatus = Gwmi Win32_PingStatus -Filter "Address ='$Server'" | Select-Object StatusCode
if ($PingStatus.StatusCode -eq 0) {
Add-Content ($filename) "write-host $Server is up -foreground green"
}
else {
Add-Content ($filename) "write-host $Server is up -foreground red"
}
}
./screendisplay.ps1 | Format-Wide {$_} -Column 4 -Force
# Gc $filename | Format-Wide {$_} -Column 4 -Force
Write-Host "`n"
Sleep -seconds (2)
}