Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script to reboot a server and email notify of when it's done
22-07-2010, 12:37 PM (This post was last modified: 22-07-2010 12:40 PM by Techmonkey.)
Post: #1
Script to reboot a server and email notify of when it's done
Bit of a long shot, but does anyone know of a program/script that will do the following.

1 - Check the list of services running on a 2003/2008 server.
2 - Send an email to specified address stating that it is about the reboot the server
3 - Reboot the server
4 - Check the list of services now running after reboot, compare against previous list
5 - Email same address confirming reboot and listing any services that have not restarted

As I said a long shot I realise Smile

- Techmonkey
================
Enterprise Business IT Support | SME IT Support | Home Support Available
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
22-07-2010, 12:37 PM
Post: #2
RE: Script to reboot a server and email notify of when it's done
Alternatively does anyone know what language would be the simplest to write this in?

- Techmonkey
================
Enterprise Business IT Support | SME IT Support | Home Support Available
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
22-07-2010, 01:44 PM
Post: #3
RE: Script to reboot a server and email notify of when it's done
To actually reboot, you could use the command:

shutdown /m \\SERVER_NAME /r /c "Restarting server in 10 seconds" /t10

/m specifies target computer, /r is restart, /c is comment and /t is time (in seconds) till reboot.

That can of course be used inside things like batch files, or (not sure what the name is) a script in the new PowerShell.

Not sure but maybe this article will help you with the services side of things:
http://www.kreslavsky.com/2009/03/manage...32008.html

As for the email, I'm sure there are plenty of applications to send from command line, but first one to spring to mind is:
http://www.febooti.com/products/command-line-email/

I would imagine you can export the services list into a file, then attach that file with the command line email. That should all be possible through a batch file.

Hopefully that points you in the right direction at least, unfortunately I only maintain Linux servers now 8-)

Register Linux Machines: 408537, 413272, 413273, 435704, 435721
Arch Linux enthusiast since 2007. Full time user since 2009
Visit this user's website Find all posts by this user
Quote this message in a reply
22-07-2010, 04:37 PM
Post: #4
RE: Script to reboot a server and email notify of when it's done
Cheers Jimmy, I will have a look and let you know how I get on.

- Techmonkey
================
Enterprise Business IT Support | SME IT Support | Home Support Available
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
03-08-2010, 04:09 PM (This post was last modified: 04-08-2010 12:53 PM by Fail Whale.)
Post: #5
RE: Script to reboot a server and email notify of when it's done
I have been working on a similar script recently to email to the helpdesk all running services prior to and after rebooting a server

What I have so far is the following triggers the reboot and send an email
Code:
#Script to get running services, email user and reboot computer and leave a tag for a script to run on boot

#get all running services and set as variable
$services = Get-Service | Where-Object {$_.status -eq "Running"}
#set variable for host name of server
$hostname = Hostname
#Set variable for system time
$time = get-date

#Send email listing server name and services stating reboot and time
#Variables for creating mail item
#From and To address
$FromAddress = "Put from address here"
$ToAddress = "put to address here"
#Set the message subject
$MessageSubject = "put subject here"
#Set the message body
$MessageBody = "The server ", $hostname ," is about to be rebooted at ", $time.DateTime ," The following services were running ", $services
#assign the correct messaging server
$SendingServer = "IP for SMTP server"
#Set credentials for sending mail
$Credentials = new-object System.Net.networkCredential  
$Credentials.domain = "your domain here"
$Credentials.UserName = $cred.username
$Credentials.Password = $cred.password
#Create Mail
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Credentials = $Credentials  
#Send
$SMTPClient.Send($SMTPMessage)

#create text file to trigger boot up script
New-Item -ItemType reboot-tag.txt

#reboot server
Restart-Computer -Force

the first script leaves a text file as a tag which if the next script set to run on boot detects it sends another email with the running services after reboot
Code:
#script to detect tag from reboot script then get services and send email
#check if reboot tag is present
$test = Test-Path -path C:\Reboot-Tag.txt
IF ($test = True)
{
#get all running services and set as variable
$services = Get-Service | Where-Object {$_.status -eq "Running"}
#set variable for host name of server
$hostname = Hostname
#Set variable for system time
$time = get-date

#Send email listing server name and services stating reboot and time
#Variables for creating mail item
#From and To address
$FromAddress = "Put from address here"
$ToAddress = "put to address here"
#Set the message subject
$MessageSubject = "put subject here"
#Set the message body
$MessageBody = "The server ", $hostname ," has come back online at  ", $time.DateTime ," The following services were running ", $services
#assign the correct messaging server
$SendingServer = "IP for SMTP server"
#Set credentials for sending mail
$Credentials = new-object System.Net.networkCredential  
$Credentials.domain = "your domain here"
$Credentials.UserName = $cred.username
$Credentials.Password = $cred.password
#Create Mail
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Credentials = $Credentials  
#Send
$SMTPClient.Send($SMTPMessage)

#Delete Reboot Tag
Remove-Item C:\Reboot-Tag.txt
}
{}

At the moment it does not work quite right as I cannot get the services to display correctly in the emails. To do your comparison between the services running before and after reboot you could possibly store the pre reboot services list in the text file used as a tag then retrieve them in the script which runs on boot up.
Quick update to fix the problem with the services not displaying in the email change
Code:
$services = Get-Service | Where-Object {$_.status -eq "Running"}
to
Code:
$services = Get-Service | Where-Object {$_.status -eq "Running"} | Out-String
and it will then display properly you can then either manualy compare the lists in the emails or pipe the results of the Get-Service command to the text file then use powershell to compare them from there.

Follow me on Twitter
My Blog
Follow me on mFlow
Gamertag - Davotronic 5000
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
03-08-2010, 05:26 PM
Post: #6
RE: Script to reboot a server and email notify of when it's done
What language is that?

Register Linux Machines: 408537, 413272, 413273, 435704, 435721
Arch Linux enthusiast since 2007. Full time user since 2009
Visit this user's website Find all posts by this user
Quote this message in a reply
03-08-2010, 05:32 PM
Post: #7
RE: Script to reboot a server and email notify of when it's done
sorry probably should have mentioned it is Powershell

Follow me on Twitter
My Blog
Follow me on mFlow
Gamertag - Davotronic 5000
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
03-08-2010, 06:19 PM
Post: #8
RE: Script to reboot a server and email notify of when it's done
No worries, thought it was Smile

Register Linux Machines: 408537, 413272, 413273, 435704, 435721
Arch Linux enthusiast since 2007. Full time user since 2009
Visit this user's website Find all posts by this user
Quote this message in a reply
03-08-2010, 10:22 PM
Post: #9
RE: Script to reboot a server and email notify of when it's done
Nice work Fail Whale, I kind of got side tracked from looking at this, but as soon as poss I will put it back on the agenda.

- Techmonkey
================
Enterprise Business IT Support | SME IT Support | Home Support Available
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: