[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell Verify that System Rebooted

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

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

829 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