Solved

Powershell: Trying to discover when a remote server has completed its reboot

Posted on 2011-02-14
5
725 Views
Last Modified: 2012-05-11
I have a script, that sometimes requires a remote server to reboot. When that reboot is finished I need the script to run another block of script. Outside of using a ping statement I am out of ides. For now this is a one to one but in the future it could be 1-100 remote servers having this script run against.
0
Comment
Question by:Mike-Po
[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
5 Comments
 
LVL 8

Expert Comment

by:jwarnken
ID: 34893225
This will work but you may want to add a service check after the ping response to ensure the system is ready to run the code.

From http://codepaste.net/7ij37r
1:  # -------------------------------------------------------------------------------------------------
   2:  # Pinger.ps1
   3:  #
   4:  # Repeatedly pings the specified server until the ping is successful.
   5:  # -------------------------------------------------------------------------------------------------
   6:   
   7:  # Script params
   8:  param([String]$server)
   9:   
  10:  # Script param validation
  11:  if ([String]::IsNullOrEmpty($server)) {
  12:      throw "You must specify a `$server to ping"
  13:  }
  14:   
  15:  $pingOutput = @()
  16:   
  17:  while ($pingOutput.Length -eq 0) {
  18:      Write-Host "Pinging $server..."
  19:      $pingOutput = @((ping $server -n 2) | where { $_ -match "TTL=[\d]{1,}" })
  20:  } 
  21:   
  22:  Write-Host "$server pinged successfully."
  23:   
  24:  # Send an email notifying that the server is back online
  25:  $smtpClient = New-Object Net.Mail.SmtpClient
  26:  $smtpClient.Host = "smtp.example.com"
  27:   
  28:  $currentUser = $env:UserName
  29:  $from = "$currentUser@example.com"
  30:  $to = "$currentUser@example.com"
  31:  $title = "$server was pinged successfully, and appears to be back up"
  32:  $body = "$server was pinged successfully, and appears to be back up"
  33:   
  34:  $smtpClient.Send($from, $to, $title, $body)

Open in new window

0
 

Author Comment

by:Mike-Po
ID: 34893340
I have writen a script similar to that, I did not even think to check for a service afterwards. I'll give it 24hrs and if someone doesnt come up with a possible cleaner solution Ill mark Jwarnken as the accepted answer.
0
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 total points
ID: 34893476
To check a serivce you can use a While statement like Jwarnken did but use the get-serivce cmdlet like this, just put in the computername and serivce you wan to check.

And instead of PING you can use the test-connection cmdlet.
While ((Test-Connection COMPUTERNAME -count 1 -q) -eq $False){
"DOWN"
}

Open in new window

While ((get-service -computername COMPUTERNAME -name netlogon -erroraction silentlycontinue).status -ne "Running"){
"Down"
}

Open in new window

0
 
LVL 6

Expert Comment

by:t-max
ID: 34893591
The only thing "different" that I can think of is to make the restarted system to run the script upon initialization.
This means that even if you have 1000 systems, every one of them will execute the start up script after they were rebooted.
This approach has no need for a "manager" script as suggested before, except for telling a computer to restart itself.
You can look here (http://technet.microsoft.com/en-us/magazine/dd630947.aspx) to see how to define computer start up scripts.
Hope this approach will help. Good luck!
0
 

Author Closing Comment

by:Mike-Po
ID: 34928752
Sorry for the delay on accepting the answer. The end result with help from KenMcF is

function Wait_For_Boot([string] $ServerName){
    While ((Test-Connection $ServerName -count 1 -q) -eq $False){
    "$ServerName not responding yet."
    Start-Sleep -s 15
    }
    While ((get-service -computername $ServerName -name termservice -erroraction silentlycontinue).status -ne "Running"){
    "Termservice not started yet."
    Start-Sleep -s 30
    }
    write-host "Server is up."
}

Ill still need to clean that up a bit, but at least Im able to procede on with my idea.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

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