Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
A safe way to clean winsxs folder from your windows server 2008 R2 editions
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…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now