?
Solved

Script to reboot servers and check availability

Posted on 2013-11-15
4
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 1000 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 41

Assisted Solution

by:footech
footech earned 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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