powershell loop to continous check if server is up

二次信任 提交于 2019-12-10 22:46:38

问题


I want to run a script to check if 5 servers are up and running based on a specific service is running.If that service is running then we know that server is up and accesible. If it does not reply with a response back then I want it to continously check for it. Heres what I got so far:

Get-Service LANMANSERVER -ComputerName JOHNJ1
Get-Service LANMANSERVER -ComputerName JOHNJ2
Get-Service LANMANSERVER -ComputerName JOHND1
Get-Service LANMANSERVER -ComputerName JOHNM
Get-Service LANMANSERVER -ComputerName JOHNI
start-sleep 90

回答1:


Yep you just need to check the Status property on the returned object like this:

$servers = "JOHNJ1", "JOHNJ2"
foreach ($server in $servers) {
    $status = (get-service -Name lanmanserver -ComputerName $server).Status
    if ($status -eq "Running") {
        "Its Up!"
    } else {
        "Its Down!"
    }
}

Update Here is an example of how to wait for a server to become online:

$servers = "JOHNJ1", "JOHNJ2"
foreach ($server in $servers) {
    while ( (get-service -Name lanmanserver -ComputerName $server).Status -ne "Running" ) {
        "Waiting for $server ..."
        Start-Sleep -Seconds 10
    }
    "$server is Up!"
}


来源:https://stackoverflow.com/questions/9098102/powershell-loop-to-continous-check-if-server-is-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!