Solved

Powershell Verify that System Rebooted

Posted on 2013-05-17
4
2,354 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
4 Comments
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 100 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 27

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 28

Accepted Solution

by:
becraig earned 200 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

707 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

18 Experts available now in Live!

Get 1:1 Help Now