Solved

Script to reboot servers and check availability

Posted on 2013-11-15
4
259 Views
Last Modified: 2014-10-06
Hello,

I am after a script which will take an input file of servers and reboot them one by one.  I need to make sure that the servers have been rebooted successfully before moving onto the next one in the list.

Does anybody know if a script like that exists?
0
Comment
Question by:ukznmcl
4 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
ID: 39651075
To start with, you can use the following script to reboot the servers and check the uptime.. Increase the sleep time as per your convenience.

$Servers = GC C:\servers.txt
$creds = get-credential Domain\admin
restart-computer -CN $Servers -force -throttlelimit 10 -credential $creds
Sleep -Seconds 600 
$Servers | % {
$Server = $_
IF (Test-Connection $Server -Quiet){
	$wmi = Get-WmiObject Win32_OperatingSystem -ComputerName $Server
	$Uptime = $wmi.LocalDateTime - $wmi.LastBootUpTime
		New-Object PSobject -Property @{
		Server = $Server
		Uptime = $Uptime
		}
	}
Else{
		New-Object PSobject -Property @{
		Server = $Server
		Uptime = "Not Pinging"
		}		
	}
}

Open in new window

0
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
ID: 39651548
How do you determine whether one has been rebooted successfully or not?  Just that you're able to ping it, or by some other criteria?  The Restart-Computer cmdlet in PS 3.0 includes a -wait parameter that can be set to check for remote PowerShell, WMI, or WinRM session (specified by the -for parameter), and wait for a positive response before moving on.  Check out the help for Restart-Computer for more detail.

To do one by one some adjustments would have to be made (just modifying Subsun's script).  I'm specifying to wait for WMI, as that is the first one available, and will still work even if you haven't set up PowerShell remoting on the target servers.
$Servers = GC C:\servers.txt
$creds = get-credential Domain\admin
foreach ($server in $servers)
{
   Restart-Computer -CN $server -force -credential $creds -wait -for WMI
}

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now