サーバーのリストを取得し、「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)
}