I looked at MS and other solutions for a way to save on the power bills and to get the computers to shut down at night. I couldn’t find anything that could fulfill my needs. Using AutoIT I wrote a Shutdown program. I called it ShutITdown.exe.
My Needs
What I did not want to do is shutdown the computer on someone who is actively working and cause them to loose their work. I wanted to give them a sufficient warning and a chance to cancel the shutdown. I wanted the Deployment, and the shutdown time, to be centrally managed. I wanted the computers to shut down instead of hibernate or suspend so a new GPO or other update would only be a day’s power on away.
Deploying ShutITdown.exe
A GPO deploys and schedules the ShutITdown.exe program via a start up script.
Creating the GPO: If you have installed the Group Policy Management Console on your server
Logged into your server Click ‘Start’ , ‘Run’ and type ‘GPMC.msc’. Find your workstations UO and right click on it and select ‘Create and Link a GPO here’. I named the GPO “ShutITdown 6PM”. Once created select ‘Edit’ which will open the ‘Group Policy’ for editing.
Creating the GPO: If you access group policies through the Active Directory Console on your server
Open your Active Directory Users and Computers console. Navigate to the target ou and right click and select ‘properties’. Click the ‘Group Policy’ tab and click the button ‘New’. A new GPO will be created called “New Group Policy Object”. Rename it. I named the GPO “ShutITdown 6PM. Then select that policy and click the button ‘Edit’ which will open the ‘Group Policy’ for editing.
Editing the GPO
Click ‘Computer Configuration’ then ‘Windows Settings’ then ‘Scripts’ and double click on ‘Start Up’. You should now be looking at “Startup Properties” dialog window. Click the ‘Show Files’ button. This will open an explorer windows with a path similar to \\DOMAINNAME.local\Sysvol\Domain.local\Policies{randoms stuff}\Machine\Scripts\Startup. You will need that exact path in a minute. Copy “ShutITdown.exe” into this exact location. And then create a file called “Setup_Shutdown6pm.bat” in this exact location. Close this Explorer Window. Go back to “Startup Properties” dialog and Click ‘Add’ then ‘Browse’ and select “Setup_Shutdown6pm.bat”. “Setup_Shutdown6pm.bat” should now be displayed on the “Startup Properties” page. Again click ‘Show Files and right click and Edit the .bat file. Copy and past the following into the .bat file.
(Beware of word wrapping. Each line is deliminated by a remark “REM LINE”. Everything between a “REM LINE” should be on one line, for a total of 7 lines.)
REM LINE 1
EVENTCREATE /SO “GPO Setup_Shutdown6PM.bat Begin” /T INFORMATION /ID 2 /L SYSTEM /D “This is a GPO start up script. A Begin entry should follow by an End entry. If not, you may have a hanging Script.”
REM LINE 2
set TaskToRun=”%windir%\ShutITdown.exe”
REM LINE 3
set DomainPath=
REM LINE 4
if not exist %TaskToRun% (COPY /Y “%DomanPath%\“ShutITdown.exe
REM LINE 5
AT /DELETE /YES
REM LINE 6
%TaskToRun%) AT 18:00 /Interactive /Every:m,t,w,th,f,s,su %TaskToRun%
REM LINE 7
EVENTCREATE /SO “GPO Setup_Shutdown6PM.bat End” /T INFORMATION /ID 2 /L SYSTEM /D “This is a GPO start up script. A Begin entry should follow by an End entry. If not, you may have a hanging Script.”
Lets walk through each line of this script.
Line 1 creates and event in the event log, basically saying that this script just started, and if you don’t see another event then the script hung and didn’t finish executing. I found this very handy in troubleshooting GPO startup scripts.
Lines 2 and 3 set variables for the correct paths. Line 2 does not need to be changed. But line 3 does. You will need to add to “set DomainPath=” to resemble “set “DomainPath=\\DOMAINNAME.local\Sysvol\Domain.local\Policies{randoms stuff}\Machine\Scripts\Startup”, aka the path to the directory where you dropped ShutITdown.exe and created this bat file. The PATH will be different based on the GPO you create.
Line 4 Checks to see if ShutITdown.exe exists on the client computer, and if not it then copies ShutITdown.exe to the executing location. This prevents the client from copying the file every time it boots and executes this script. The ShutITdown.exe program is fairly small and wouldn’t cause any noticable network traffic or excessive boot time. But this is good practice. If you update the ShutITdown.exe program and need to deploy it to the clients, you will need to REM out this line and create a new line only including the text within the parenthesis for the first few boots.
Line 5 Clears the AT que. If you don’t do this, then every time the computer restarts there will be an additional “AT 18:00” event. Note: This may break other AT schedules on your clients.
Line 6 Sets ShutITdown.exe to be launched by AT at the given time.
Line 7 Again creates an Event so you know that the script completed.
Why did I choose to use AT? Because schtasks cannot execute in user space whereas AT can. Trust me, it didn’t work. AT is what executes the ShutITdown.exe program (at 18:00) and the program then does its thing.
The ShutITdown.exe Program Doing its thing
ShutITdown.exe first checks to see if a user is logged in. If no one is logged in then it shuts down the computer. If a user is logged in, it checks to see if the user has been idle for the past 15 minutes. If not, it then waits for 15 more minutes and checks again to see if the user has been idle. It will check every 15 minutes for 2 hours, and then give up. If the user has been idle for a 15 minute block the program then warns any logged in user, with a message box that the computer is going to shut down soon. It waits for 3 minutes (I think) for a response and then it shuts down the computer. Due to the effectiveness of the idle timer, I haven’t heard of anyone actually seeing the message box before.
This is working mostly flawlessly on our XP domain. It has not been tested on Vista or Windows7. I am curious about the results though. Deployment does require the computers to be restarted manually once, so it gets and executes the start up GPO script. If you have any problems setting it up, I’d be glad to help you out with it for a small consultation fee.
If there are any requests for the source for ShutItdown then maybe I will supply it. It is written in AutoIT v3.
Here is the link for the ShutITdown.exe
No, it all goes into the start up folder. It is a startup script that assigns schedules the AT program to execute the ShutITdown.exe.
Troubleshooting The Script
Goto a client computer and check the event logs and see if you have the start up script events (mentioned above). If not, then the GPO script is not executing. If the script is not executing then the AT command is not getting assigned. Troubleshoot the start up script and the GPO. Execute the start up script buy copying it right out of the GPO Folder then edit the script and add the single word command “pause” to the end and then execute the script while logged in locally. Then check the event logs. Troubleshooting the GPO is beyond the scope of this article. You can double check that the client computer is in the proper OU and that the GPO is linked to that OUT and that no other GPO conflicts with this GPO.
Troubleshooting the scheduler; AT.
Open a command prompt and type “AT” then enter. AT with no arguments lists all AT scheduled tasks. Do you see an AT job for ShutITdown.exe? Is it scheduled at the correct time? try scheduling a new time (by copying and pasting line 6 (above) but modify the time to a time within the next few minutes. Execute the AT command then wait for the assigned time to pass and see what happens. Remember that ShutITdown.exe sits silently waiting for 15 minutes after executing. So just after the scheduled time, you will need to open the task manager and look for a task running called ShutITdown.exe.
Troubleshooting the Shutdown
When the ShutITdown.exe attempts a shutdown, that is, all conditions were met for a shutdown, the program will write an event to the event log. It will say something along the lines of “ShutITdown has attempted a shutdown..” If ShutITdown.exe excutes (ie, loads into the task manager) but this event does not show up in the event log, then something is preventing the conditions for ShutITdown.exe from performing a shutdown. It could be a security program, the system not going idle for the required 15 minute block etc. If you do see this entry in the event log and the computer isn’t shutting down then something is definatly screwed up. It could be a permissions issue, but AT should be scheduled with system authority and At should execute ShutITdown and ShutITdown should execute the shutdown call.
___________

If you found this information helpful and saved you time or energy how about you buy me a beer by clicking the donate button below!