How to schedule .bat to run at shutdown

Discussion in 'other software & services' started by philby, May 14, 2010.

Thread Status:
Not open for further replies.
  1. philby

    philby Registered Member

    Joined:
    Jan 10, 2008
    Posts:
    940
    Hello

    After struggling a little with creating a task earlier on, I've now got a little schedulable batch (robocopy /mir) file scheduled to backup an Outlook .pst file.

    What I need to be able to do is to use Task Scheduler to have this batch file run just as shutdown is invoked.

    I don't mean run the batch and immediately shut down - I know I can do that by adding a line to the batch file.

    I just want the shutdown command to invoke the batch, if you know what I mean, so that the PST will be copied immediately prior to shutting down Windows.

    I can't use GPEdit as I'm running 7 Home Premium.

    Thanks in advance.

    philby

    PS - Yes, I have tried FreeFileSync in combination with the vss service to backup the .pst on the fly, but vss went nuts, so i'd prefer to avoid.
     
  2. Sully

    Sully Registered Member

    Joined:
    Dec 23, 2005
    Posts:
    3,719
    I don't understand why you don't want to just create a batch file that does your business and then give the shutdown command. There are options with the shutdown command that might be of use for you. What is the difference whether you use the "start" button to shut down or execute a script to do it?

    You could compile the batch file into an executable, and give it an icon. You could also use other script languages for more refined control over the shutdown process. You could use AutoIt, which will create a GUI for you if you like as well as a standalone executable.

    I was just wondering why you stipulate that when the end result will be the same as long as you don't script a forced shutdown.

    Sul.
     
  3. philby

    philby Registered Member

    Joined:
    Jan 10, 2008
    Posts:
    940
    Hi Sully

    I know it seems odd: I'm actually trying to help a shop-owner friend who will never remember to hit anything other than start > shutdown at the end of the day.

    That's why I thought using the TS to sneak the batch file in just before normal shutdown would do the trick - I'm very surprised that this isn't a trigger option in TS.

    I've just tried using the trigger 'on disconnect from user session' (local computer) but Windows didn't like that at all.

    Edit: Is there a way I could adapt shutdown.exe to include the batch file run such that the normal start > shutdown procedure would have the desired effect?

    philby
     
    Last edited: May 14, 2010
  4. Sully

    Sully Registered Member

    Joined:
    Dec 23, 2005
    Posts:
    3,719
    I don't think that without the GP you can do this with a type of "on event". I don't know that task scheduler can do it either.

    One way you could do it, if the user will not train themselves to hit a special icon, is to replace shutdown.exe with one of your own. For example, rename shutdown.exe to shutdown_org.exe, and compile a script to be named shutdown.exe. In the script the, call shutdown_org.exe with the proper command line arguments. Not very elegant, but without the GP, this kinda sucks.

    There might be another way, perhaps in the registry. If you can find a rig with the GP working, it may be that there is a registry value that can be employed. Someone else may know something as well, but a quick look shows me only the GP can "catch" the event.

    Sul.

    EDIT: I am not sure, but I think scripts like AutoIt use calls to libraries etc to do the shutdown, rather than to actually need shutdown.exe. You might look into that. If I can of assistance let me know. Should not be too hard.
     
  5. doktornotor

    doktornotor Registered Member

    Joined:
    Jul 19, 2008
    Posts:
    2,047
    Well, with a lame Windows version w/o group policy editor, you'd need either some third-party resident utility or teach the guy to click on an icon (I mean, how hard exactly is that?)

    If the only thing you are trying to do is to backup his Outlook stuff, you don't need any of this though. Simply use Outlook 2007/2003/2002 Add-in: Personal Folders Backup
     
  6. philby

    philby Registered Member

    Joined:
    Jan 10, 2008
    Posts:
    940
    OK, thanks - I'll take a look at that MS util.

    Regarding the icon option, my batch file currently reads:

    start /min robocopy Outlook filepath /mir Backupfilepath

    but I don' t seem to be able to get the shut down line correctly placed.

    I've tried this:

    start /min robocopy OutlookfFilepath /mir Backupfilepath
    Exit
    shutdown -s -f t 00

    but it doesn't work. The backup runs but there's no shutdown.

    I've also tried this:

    start /min robocopy Outlook filepath /mir Backupfilepath
    C:\windows\system32\shutdown.exe -s t 00
    but same problem.

    I can't figure out what line to add...

    (Forgive me - I have zero knowledge of scripting, but am trying).

    Thanks all.

    philby
     
  7. doktornotor

    doktornotor Registered Member

    Joined:
    Jul 19, 2008
    Posts:
    2,047
    LOL. remove the exit thing and if needed add /wait to the start command. ;) Also, missing - before the t, i.e.
    Code:
    shutdown -s -f -t 00
     
  8. philby

    philby Registered Member

    Joined:
    Jan 10, 2008
    Posts:
    940
    I'm glad I was entertaining with my coding attempts... :oops: If anyone needs any help, I'm your man o_O

    I added the code you provided and it's sorted. Thanks very much for your help.

    I will train the staff at the shop on how to forgo the start button for the new icon.

    philby
     
  9. doktornotor

    doktornotor Registered Member

    Joined:
    Jul 19, 2008
    Posts:
    2,047
    Hello, good that it works. Also, I managed to find out this registry trick (i.e., no group policy required) to remove shutdown/reboot options from start menu, which could assist you in teaching :D

    Remove Shutdown Options From Windows 7 Start Menu

    HTH.
     
  10. Cudni

    Cudni Global Moderator

    Joined:
    May 24, 2009
    Posts:
    6,956
    Location:
    Somethingshire
    if you have time experiment with built in backup Win7 facilities + it might be just a bit safer to backup/copy .pst on user logon (as outlook will/can not be running)
     
  11. doktornotor

    doktornotor Registered Member

    Joined:
    Jul 19, 2008
    Posts:
    2,047
    Could as well use pskill.exe from Sysinternals Suite to kill Outlook before running the command. (Do not forget to add the undocumented -accepteula switch to the pskill command or you'll end up scratching your head very much about the M$ stupidity.)
     
  12. MrBrian

    MrBrian Registered Member

    Joined:
    Feb 24, 2008
    Posts:
    6,032
    Location:
    USA
    Maybe LastChance works on Windows 7. Then again, maybe it doesn't.
     
  13. hossie

    hossie Registered Member

    Joined:
    Nov 8, 2005
    Posts:
    88
    I think a batch file can be created and kept under C:\WINDOWS\Tasks and you can schedule it at a specific time
     
Loading...
Thread Status:
Not open for further replies.