Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-14
5
Medium Priority
?
727 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

618 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