Solved

Powershell Verify that System Rebooted

Posted on 2013-05-17
4
2,754 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 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 29

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 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

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

730 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