Jere's Techblog

Bulk reboot Server

With this variant, the servers from a list or an array will be restarted sequentially. If a server is not reachable or has problems with the Windows-Remoting-Service, this can lead to long runtimes. It gives you a nice overview where the reboot job worked or not.

With the parameter “-force” the servers will be rebooted even if there is still an active user session.

#13.11.2018 Restart a list/array of Servers through Windows Remoting

$server = @(
"Hostname-Server1"
"Hostname-Server2"
"Hostname-Server3"
)


foreach ($server in $server){
    try{
        Restart-Computer -ComputerName $Server -force
        write-host "Reboot OK $server" -ForegroundColor Green
    }catch{
        write-host "Reboot NOT OK $server" -ForegroundColor yellow
          }

}

This is the parallel way to reboote servers from a list/array as a job using the “Invoke” function.

#13.11.2018 Restart a Liste/Array of Servers through Windows Remoting

$server = @(
"Hostname-Server1"
"Hostname-Server2"
"Hostname-Server3"
)


foreach ($server in $server){
Invoke-Command -ComputerName $Server -ScriptBlock {shutdown -r -f -t 1} -AsJob  
}

With this function you can check if the servers have been restarted. You can also Check the last boot time.

#13.11.2018 Restart a Liste/Array of Servers through Windows Remoting

$array = @()
$server = @(
"HostnameServer-1"
"HostnameServer-2"
"HostnameServer-3"
)

foreach ($server in $server){

    IF($s= New-CimSession -ComputerName $server -ErrorAction SilentlyContinue){
        $array += (Get-CimInstance -ClassName win32_operatingsystem -CimSession $s ) #| select csname, lastbootuptime
    }Else{
        $myObject = [PSCustomObject]@{
            PSComputerName     = $server
            csname     = $server
            lastbootuptime = 'no data retrieved'
            }
        $array += $myObject
    }
}

Function Checkreboottime{

Param(
  [Parameter(Mandatory=$true)]
   [int]$time
)


$TimeNow = Get-Date

$array | % {
    IF(!($_.lastbootuptime -eq "no data retrieved")){
        IF ([dateTime]$_.lastbootuptime.AddMinutes($time) -ge $TimeNow){
            write-host $_.csname $_.lastbootuptime -ForegroundColor Green
        }Else{
            write-host $_.csname $_.lastbootuptime -ForegroundColor yellow
        }
    }Else{
        write-host $_.csname $_.lastbootuptime -ForegroundColor Cyan
        }
    }

}

After calling the script, the function “Checkreboottime” can be used to check which servers have been restarted within a certain time.

Example: Checkreboottime -time 1000

The value 1000 indicates the minutes.

Yellow = Computer has not been restarted for more than 1000 minutes (since the time the script was executed)

Blue= No values could be determined

Green= computer restarted within 1000 minutes (since the script was executed)

Continue reading...