实时控制台屏幕

时间:2012-11-27 17:38:15

标签: powershell

有没有办法获取服务器列表,执行“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)    
}

0 个答案:

没有答案