Solved

Script to reboot servers and check availability

Posted on 2013-11-15
4
266 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 40

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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