sämtliche Computer der Active-Directory neu starten

powershell placeholder

Das Script liest die AD-Computer aus. Der Filter wird in der Option „-filter ‚Name -like …*“ definiert. Bevor der Neustartbefehl (shutdown.exe) ausgeführt wird, wird geprüft, ob der Computer anpingbar ist. Das reduziert die Fehlermeldungen wenn ein Computer nicht angeschaltet ist. Nach 30 S.stemen stoppt das Script für 60 Sekunden. Wie immer verschlinkt das ganze Drumherum im Script die meisten Zeilen 😉

Script – Computer der Active-Directory neu starten

$debug_dont_execute_shutdown_cmd=1

Write-Host "Press Enter to restart all Kiosks in groups"
pause 
#

$results = Get-ADComputer -Filter 'Name -like "office*"'  
$current_number=0

foreach ($ad_client in $results) {

    $current_number++
    write-host "- current host: "$ad_client
    
            if (-not (Test-Connection -ComputerName $ad_client -count 1 -Quiet)){

                $Pingcheckresult_failed = 1;
                Write-Host " -> Pingcheck failed! skip server "$ad_client

            }else{
                $tmp_unc_hostname="\\"+$ad_client

                 write-host " -> reboot device: "$tmp_hostname_generic
                                  
                 if($debug_dont_execute_shutdown_cmd -eq 1){
                 
                    shutdown.exe /m $tmp_unc_hostname /r /t 0
                 }
            }

        if($current_number -eq 30 ){
            
            write-host " !! waiting 60 secounds..."
            Start-Sleep -s 60
       
        } 
        if($current_number -eq 60 ){
            
            write-host " !! waiting 60 secounds..."
            Start-Sleep -s 60    
        } 
        if($current_number -eq 90 ){
            
            write-host " !! waiting 60 secounds..."
            Start-Sleep -s 60
        } 
}

Script – Variante ohne AD-Anbindung mit generischen Namen

$count_of_devices=100
$hostname_generic="kioskXX"
$debug_dont_execute_shutdown_cmd=1

Write-Host "Press Enter to restart all Kiosks in groups"
pause 

for ($first_host_number=1;$first_host_number -le $count_of_devices; $first_host_number++) {

    $current_number=$first_host_number
    if($current_number -lt 10){$current_number="0"+$current_number}

    $tmp_hostname_generic=$hostname_generic
    $tmp_hostname_generic=$tmp_hostname_generic -replace 'XX',$current_number

    write-host "- current host: "$tmp_hostname_generic
    
            if (-not (Test-Connection -ComputerName $tmp_hostname_generic -count 1 -Quiet)){

                $Pingcheckresult_failed = 1;
                Write-Host " -> Pingcheck failed! skip server "$tmp_hostname_generic

            }else{
                $tmp_unc_hostname="\\"+$tmp_hostname_generic

                 Write-Host " -> Pingcheck successful: "$tmp_hostname_generic
                 write-host " -> reboot device: "$tmp_hostname_generic
                                  
                 if($debug_dont_execute_shutdown_cmd -eq 1){
                 
                    shutdown.exe /m $tmp_unc_hostname /r /t 0
                 }
            }

        if($current_number -eq 30 ){
            
            write-host " !! waiting 60 secounds..."
            Start-Sleep -s 60
        } 
        if($current_number -eq 60 ){
            
            write-host " !! waiting 60 secounds..."
            Start-Sleep -s 60
        } 
        if($current_number -eq 90 ){
            
            write-host " !! waiting 60 secounds..."
            Start-Sleep -s 60
        } 

}

einen Dienst auf den AD-Computern neustarten mit SC.exe

Natürlich kann man das Script auch anpassen, sodass ein spezieller Dienst auf eine Auswahl von AD-Computern neugestartet wird. Ohne Pingcheck geht das bestimmt noch eleganter als Oneliner.

$debug_dont_execute_shutdown_cmd=1

$results = Get-ADComputer -Filter 'Name -like "office*"'  

foreach ($ad_client in $results) {


    write-host "- current host: "$ad_client
    
            if (-not (Test-Connection -ComputerName $ad_client -count 1 -Quiet)){

                $Pingcheckresult_failed = 1;
                Write-Host " -> Pingcheck failed! skip server "$ad_client

            }else{
                $tmp_unc_hostname="\\"+$ad_client

                 write-host " -> stop NSClient++: " $cmd_stop
                 $cmd_stop = sc.exe $tmp_unc_hostname stop nsclientpp
                 
                 $cmd_start = sc.exe $tmp_unc_hostname start nsclientpp    
                 write-host " -> start NSClient++: " $cmd_start           
            }
}

weitere Powershell Scripte