1

I have 2 batch files that send emails intended to notify of a server shutdown or startup. The batch files call an exe with a single argument, the message to be sent. The exe comes from a vb.net program using system.net.mail.

The batch files work properly from a command prompt and emails are received.

Now I want these batch files to run automatically when the OS starts and when the OS is being shutdown, without any user having to log on.

I tried testing this on a Win 8.1 machine which I believe should work the same as on Server 2012 R2. If this is a bad assumption, please advise.

So here's what I tried:

1) Task Scheduler (but only for startup, there's no shutdown option)

2) Local Group Policy Editor > Computer Configuration > Windows Settings > Scripts (Startup/Shutdown)

In both cases the setup seemed pretty straightforward, but the emails are never sent.

Can someone give me ideas about considerations I clearly am not thinking about?

Is the problem permissions? How would I know that?

Is the problem "It looks scheduled but isn't"?

Is the problem that batch files can't be used in this way?

Since this is my first time doing this, I'm pretty certain I'm missing something basic.

3
  • You could do a simple test using a one-liner startup/shutdown script written in powershell instead, google the send-mailmessage cmdlet for syntax. This will let you adjust connectivity parameters more easily by the sound of it. The startup/shutdown scripts GPO node exists for these purposes, so you ought to be on the right track. – ErikE May 1 '15 at 20:32
  • I successfully composed a powershell script using the send-mailmessage cmdlet; you were right, it's easy to use. Unfortunately, while my email script works perfectly, I still cannot any scripts to run at any point during machine startup. – Alan May 4 '15 at 22:30
  • Ooo, you made me curious now. I'll do a simple test myself at earliest convenience. – ErikE May 6 '15 at 22:39
1

This really isn't an answer due to the two limitations noted below but it's too long for a comment, so..

I did a simple test with the following two shortcomings:

  1. The scripts do not send email. I need the smtp relay of my ISP but it's too much hassle to find it.
  2. I'm testing on a Win10 preview, not a Win8.1. It's all I have this evening.

Inside of Local Group Policy Editor > Computer Configuration > Windows Settings > Scripts (Startup/Shutdown), I have put one startup powershell script in the startup directory and one shutdown powershell script in the shutdown directory.

I have taken special care to add the scripts under the Powershell Scripts tab inside each of the startup and shutdown policy nodes.

I have made sure I have a directory called c:\temp as I was too lazy to script it into existence.

I have also made sure powershell scripts are allowed by running:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Here is the startup script:

if (!(Test-Path c:\temp\log.txt)) {New-Item -ItemType file -Path c:\temp\log.txt}
$timestamp = get-date 
$string = "$timestamp UP!"
$string | Out-File -FilePath c:\temp\log.txt -Append -NoClobber

Here is the shutdown script:

if (!(Test-Path c:\temp\log.txt)) {New-Item -ItemType file -Path c:\temp\log.txt}
$timestamp = get-date 
$string = "$timestamp DOWN!"
$string | Out-File -FilePath c:\temp\log.txt -Append -NoClobber

I then rebooted the computer. Here is the content of the c:\temp\log.txt file:

06/01/2015 00:07:45 DOWN!
06/01/2015 00:08:10 UP!

I would expect the same result with having it sending a mail, but one can't be sure of course until one actually tries..

1

Studying and using everyone's comments and answers actually helped resolve my problem, but as sometimes is the case the real issue was somewhat different. So, I feel obligated to post what turned out to be the actual problem for me in case someone else gets caught by the same 'gotcha'.

During script testing it seemed more convenient to work on my Win 8.1 machine; turns out it wasn't. I had assumed my Win 8.1 machine would in fact work identically to server 2012 R2; my assumption was wrong.

The instant I moved everything over to my 2012 R2 servers the scripts worked flawlessly and have ever since. I cannot explain why the Win 8.1 machine reacted differently, it just did.

So my solution was to no longer use a Win 8 or 8.1 machine for testing startup/shutdown scripts, just do it on the target server.

(Sorry for the delayed response, I was out of town on an extended trip)

-2

Go to the eventviewer, search for logon and logoff security events, right click on it and add a task for this event which sends an email.

Its the best way.

4
  • Logon/logoff is not startup/shutdown. – ErikE May 1 '15 at 20:34
  • .... Than look the System eventlog for the shutdown events – Cosmic542 May 3 '15 at 6:14
  • Looking at the logs, I don't understand how to select an event. Looking in the event viewer I see Security, System, etc. logs, but I don't know how I would pick an event that ensures the machine is through startup or shutdown is imminent. Is any further guidance possible? – Alan May 4 '15 at 22:33
  • Attaching tasks via Event Viewer has been removed in Server 2012. – I say Reinstate Monica Jun 4 '15 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.