Windows 7 - How to run a script at shutdown but not at logoff

I was wondering if anyone knows how to configure a Windows 7 x64 Enterprise install that is on a domain to run a script if the user attempts to shutdown the computer - but I dont want the script to run if the user tries to log out.

Here is a bit of detail on my situation...

I have installed drive encryption software on 5 desktops that are in use by various people in my group. These desktops are installed a few buildings away in a secure lab that very people have access to. People in my group access these machines (8 Core 2600K Intel Sandybridge with 8GB RAM) over RDP to preform tasks that require heavy lifting that laptops are not well suited for.

I wrote a shutdown script (.hta file) to warn a user that if he/she shuts the computer down, they will have to get access to the lab and enter the drive encryption password in order to bring the machine back online. The script has a timer that counts down for 1 minute and allows the user to cancel the shutdown command.

The script works as expected when a user tries to shutdown the computer, but I just realized that the script seems to run when a user tries to log off. Is there any way to differentiate between a user logging off and a user shutting down a machine? If possible, I would only want the script to run if the machine is shutdown. If the user logs out, I would not want any script to run.

If this is possible, it seems like this would be managed via local Group Policy.

Let me know if you need any further details.


Solution 1:

There are two ways to achieve this. If you have an Active Directory domain this would be what I advise as it would be the easiest to configure. You can just add these special computers to their own OU and apply a GPO to the OU. Otherwise you will need to apply the shutdown script using the Local Policy manager on each individual computer:

Shutdown Scripts through Group Policy:

http://technet.microsoft.com/en-us/library/cc783802%28v=ws.10%29.aspx

Shutdown Scripts through Local Policy:

http://technet.microsoft.com/en-us/library/cc770300.aspx

However by default Shutdown output aren't visible by default. You will need to make another change the policies to make sure they are:

http://technet.microsoft.com/en-us/library/cc940430.aspx

I'm not sure if an HTA will work, but if you just want to make sure a message displays prior to shutting down you could easily convert to a .vbs or .bat file.

Solution 2:

So, I figured it out myself - but not without everyone's help - so thanks.

I just noticed the following

There are GPOs for both Shutdown and Logoff scripts

Log off scripts are in User Configuration/Windows Settings/Scripts (Log on and Log off)

Shutdown scripts are in Computer Configuration/Windows Settings/Scripts (Startup/Shutdown)