Solved

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

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
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…

830 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