[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell Verify that System Rebooted

Posted on 2013-05-17
4
Medium Priority
?
3,475 Views
Last Modified: 2013-05-24
Hi,

I have a snippet of a reboot script below - It will force a shutdown on a list of servers, pause for 90 seconds, then check to ensure that the servers are up. Now I can foresee condition where the system hangs at reboot, yet passes the availability check. I can think of a few ways to avoid this situation, i.e. check system uptime, and if uptime is less than x minutes, dump the script. I am requesting some assistance on the best way to check that a system actually rebooted prior to continuing the script. Thank you.

$Time = (get-date -f yyyy-MM-dd_HH.mm.ss)
LogWrite "Server reboots initiated at $Time"

# Gets the server reboot list

$Servers = Get-Content C:\servers.txt
foreach ($Server in $Servers)
{
LogWrite “Issuing remote reboot command to $Server”

# Command to force reboot the remote server
(Get-WmiObject Win32_OperatingSystem -ComputerName $Server).Win32Shutdown(6)
LogWrite "$Server successfully sent restart command"
}

Start-Sleep -s 90

LogWrite "****************************************************"
LogWrite "**************CHECKING ONLINE STATUS****************"
LogWrite "****************************************************"

foreach ($Server in $Servers)
{
    while ( (get-service -Name lanmanserver -ComputerName $Server).Status -ne "Running" ) {
LogWrite "Waiting for $Server ..."
        Start-Sleep -s 15
    }
LogWrite "$Server is Up!"

}
0
Comment
Question by:wicked212
[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 42

Assisted Solution

by:Meir Rivkin
Meir Rivkin earned 400 total points
ID: 39176777
i used this script for remote reboot:
http://www.powershellmagazine.com/2012/11/27/better-restart-computer-cmdlet-in-powershell-3-0/

it has -wait argument which waits until the specified service or feature is available after the computer is restarted for a set of predefined values.
i use WinRM parameter which means that a remote session can be established to the remote server.
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 39177352
Instead of checking for the service how about running a ping command?
0
 
LVL 30

Expert Comment

by:serialband
ID: 39177530
You can download the uptime.exe utility from Microsoft to report uptimes.  You'll have to put it in the path on the remote systems.

If you don't want to do that, try piping systeminfo to the find command.

systeminfo|find "System Boot Time"
0
 
LVL 29

Accepted Solution

by:
becraig earned 800 total points
ID: 39177631
Here is a an option to add to your script using powershell

$uptime = (Get-WmiObject -Class Win32_OperatingSystem -computername $server).LastBootUpTime
$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($uptime)

if (($sysuptime.Minutes -GT 10) -and ($sysuptime.Hours -LE 0)) {}

Open in new window


You can probably add some logic to do a wait then a do while Test-connection which if the server responds to the test-connection then you test the uptime.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

656 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