?
Solved

Powershell Verify that System Rebooted

Posted on 2013-05-17
4
Medium Priority
?
3,266 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:sedgwick
sedgwick 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

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

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.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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 the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
Suggested Courses

765 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