I have this backup script that shuts down Outlook, runs SyncToy, then restarts Outlook. It runs perfectly when started interactively but doesn't run correctly when scheduled. This is Windows 7 Enterprise, btw.
The scheduler says the task has been run correctly, but the backups aren't happening and the log isn't getting created on the desktop. The task is configured to run as my user, and checking the security logs shows that the logon is succeeding.
When I exec the task manually from the scheduler while logged on as the same user here's what happens:
- task scheduler says the task has been started and gives a pid for cmd.exe
- that pid doesn't show up in task manager unless you click "show processes from all users", even though it is run under my account, and all other processes run under "mark" do show up.
- Outlook doesn't close.
- SyncToy doesn't start, and no log file is written to the desktop.
- The cmd.exe instance doesn't exit cleanly.
Previously I had SyncToy scheduled directly from task scheduler and it worked fine. I added this script to solve the problem of Outlook holding a lock on my mail file. What I assume is happening is that the task scheduler is logging my user onto a hidden window session and running the task in that, but I am not sure why. It further seems that something about the environment in that case is causing the nircmd.exe call to close outlook to fail, and that is parking the script in the wait loop.
Anyone have any ideas?
Code:
@echo off
echo Synchronizing folders...
echo Shutting down Outlook.exe...
"c:\Mark Personal\System Tools\NirCmd\nircmd.exe" closeprocess outlook.exe
:wait
timeout 2
tasklist /FI "IMAGENAME eq outlook.exe" 2>NUL | find /I /N "outlook.exe">NUL
if "%ERRORLEVEL%"=="0" goto :wait
echo Outlook closed; executing SyncToyCmd...
"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R > c:\users\mark\Desktop\Sync.log
echo Sync completed; starting Outlook.exe...
start "" /B /MIN C:\Users\Mark\Desktop\Outlook
echo Outlook.exe started; exiting
@echo on
The scheduler says the task has been run correctly, but the backups aren't happening and the log isn't getting created on the desktop. The task is configured to run as my user, and checking the security logs shows that the logon is succeeding.
When I exec the task manually from the scheduler while logged on as the same user here's what happens:
- task scheduler says the task has been started and gives a pid for cmd.exe
- that pid doesn't show up in task manager unless you click "show processes from all users", even though it is run under my account, and all other processes run under "mark" do show up.
- Outlook doesn't close.
- SyncToy doesn't start, and no log file is written to the desktop.
- The cmd.exe instance doesn't exit cleanly.
Previously I had SyncToy scheduled directly from task scheduler and it worked fine. I added this script to solve the problem of Outlook holding a lock on my mail file. What I assume is happening is that the task scheduler is logging my user onto a hidden window session and running the task in that, but I am not sure why. It further seems that something about the environment in that case is causing the nircmd.exe call to close outlook to fail, and that is parking the script in the wait loop.
Anyone have any ideas?
Last edited: