Solved

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

Posted on 2011-02-14
5
724 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

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.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
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…
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…

738 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