mycomputerworks
asked on
Scheduled Task won't close Outlook
I am running Windows7 64 bit and Outlook 2010
I have a batch file that produces a different result if I double click on it vs if I run it from Task Scheduler
I have at .bat file that has the following command:
cscript.exe CloseOutlook.vbs
The command is supposed to close any running Outlook Processes.
The contents of CloseOutlook.vbs:
If I double click on my batchfile it works perfect. However if I schedule it in Task Scheduler it doesn't close outlook. If no Outlook is running it it does give me the message, "Process outlook.exe is NOT running on computer " But if Outlook is running it apparently finds it and does nothing.
In Task Scheduler I have "Run with highest privileges" set.
I have tried running it "only when user is logged on" and "whether user is logged on or not"
The batch file has other commands and it runs to completion without error, but Outlook remains open.
I have a batch file that produces a different result if I double click on it vs if I run it from Task Scheduler
I have at .bat file that has the following command:
cscript.exe CloseOutlook.vbs
The command is supposed to close any running Outlook Processes.
The contents of CloseOutlook.vbs:
Function IsProcessRunning( strServer, strProcess )
Dim Process, strObject
IsProcessRunning = False
strObject = "winmgmts://" & strServer
For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
If UCase( Process.name ) = UCase( strProcess ) Then
IsProcessRunning = True
Exit Function
End If
Next
End Function
On Error Resume Next
Dim strComputer, strProcess
strProcess = "outlook.exe"
strComputer = "."
If( IsProcessRunning( strComputer, strProcess ) = True ) Then
Set Outlook = GetObject(, "Outlook.Application")
If Err = 0 Then
Outlook.Quit()
End If
Else
WScript.Echo "Process " & strProcess & " is NOT running on computer " & strComputer
End If
If I double click on my batchfile it works perfect. However if I schedule it in Task Scheduler it doesn't close outlook. If no Outlook is running it it does give me the message, "Process outlook.exe is NOT running on computer " But if Outlook is running it apparently finds it and does nothing.
In Task Scheduler I have "Run with highest privileges" set.
I have tried running it "only when user is logged on" and "whether user is logged on or not"
The batch file has other commands and it runs to completion without error, but Outlook remains open.
ASKER
My understanding is that Taskkill.exe can cause outlook to go down in an ungraceful way and sometime break the .pst file.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm stuck on this, too. I need to close Outlook every morning and while my .bat/.vbs script works fine from a command line (not even one with elevated permissions) I can't get it to work from Task Scheduler. It seems like the problem is with GetObject() - when running from a command prompt it returns a reference to Outlook but when executed via Task Scheduler it returns nothing.
Here's my .bat file:
Here's my .vbs file:
Here's the output in vbs_log.txt when run from command prompt:
Here's the output in vbs_log.txt when executed from Task Scheduler:
Why doesn't the GetObject() call work from Task Scheduler? I have the task set up to 'Run only when user is logged on', 'Run with highest privileges' and the Start In is set to the same folder as the .bat & .vbs files.
Here's my .bat file:
@echo off
echo "==========================================" >> d:\outlook\batch_log.txt
echo %date% %time% >> d:\outlook\batch_log.txt
echo "Closing Outlook via vbs ..." >> d:\outlook\batch_log.txt
cscript //Nologo //B D:\Outlook\Close_Outlook.vbs >> d:\outlook\batch_log.txt 2>>D:\Outlook\errorlog.txt
Here's my .vbs file:
Function IsProcessRunning( strServer, strProcess )
Dim Process, strObject
IsProcessRunning = False
strObject = "winmgmts://" & strServer
For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
'objFile.WriteLine Process.name
If UCase( Process.name ) = UCase( strProcess ) Then
IsProcessRunning = True
Exit Function
End If
Next
End Function
on error resume next
Dim oOL 'As Outlook.Application
Dim objFSO 'As FileSystemObject
Dim objFile 'As File
Dim strComputer, strProcess
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\Outlook\vbs_log.txt", True)
objFile.WriteLine "VBS: " & Now
strProcess = "outlook.exe"
strComputer = "."
If( IsProcessRunning( strComputer, strProcess ) = True ) Then
objFile.WriteLine "IsProcessRunning=TRUE"
Set oOL = GetObject(, "Outlook.Application")
'Set oOL = CreateObject("Outlook.Application")
'WScript.Sleep 5000
If oOL Is Nothing Then
'no need to do anything, Outlook is not running
objFile.WriteLine "GetObject() result is nothing"
Else
'Outlook running
objFile.WriteLine "GetObject() result is NOT nothing, attempting to log off from session and quit"
If Err = 0 Then
oOL.Session.Logoff
oOL.Quit
End If
End If
Else
objFile.WriteLine "IsProcessRunning=FALSE"
End If
objFile.Close
Set oOL = Nothing
Set objFile = Nothing
Set objFSO = Nothing
Here's the output in vbs_log.txt when run from command prompt:
VBS: 07/30/2014 2:34:51 PM
IsProcessRunning=TRUE
GetObject() result is NOT nothing, attempting to log off from session and quit
Here's the output in vbs_log.txt when executed from Task Scheduler:
VBS: 07/30/2014 2:33:44 PM
IsProcessRunning=TRUE
GetObject() result is nothing
Why doesn't the GetObject() call work from Task Scheduler? I have the task set up to 'Run only when user is logged on', 'Run with highest privileges' and the Start In is set to the same folder as the .bat & .vbs files.
I added some error handling to the .vbs script and ONLY when I run this from Task Scheduler do I see this:
Error 429: ActiveX component can't create object
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks Joe. It works perfectly although I'd rather use vbscript. Until I can figure out what's wrong with GetObject(), though, I'll use your solution.
Ross,
I'm glad to hear that it works perfectly for you. Strange that an AHK script works fine in a BAT file called via TS but a VBScript does not. Please post back here when you figure out the problem. I'm curious, although the original asker has obviously lost interest. Regards, Joe
I'm glad to hear that it works perfectly for you. Strange that an AHK script works fine in a BAT file called via TS but a VBScript does not. Please post back here when you figure out the problem. I'm curious, although the original asker has obviously lost interest. Regards, Joe
The same .vbs script works on a colleague's machine so perhaps it's a problem with the MS Office install on my box.
Very interesting!
While the AHK script works perfectly when I'm logged on and working on the desktop, it doesn't seem to work when I lock it at night (still logged in, though).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That worked! Thanks Joe. I hadn't considered changing this setting as my research showed it needed to be 'Run only when users is logged on' for the aborted .vbs attempt (which is also true for my colleague who was able to use the .vbs version).
That's great news, Ross. Did you have to store the password?
No, I checked the box about not needing it and it seemed to work.
Excellent!
Hi Ross,
I'm trying to clean up some open questions and noticed that I haven't heard from you in nearly three months on this one. Back on 1-Aug, it seemed that everything was resolved, so I'd appreciate it if you close out the question. However, if there are still open issues, please let me know and I'll try to continue helping. Thanks, Joe
I'm trying to clean up some open questions and noticed that I haven't heard from you in nearly three months on this one. Back on 1-Aug, it seemed that everything was resolved, so I'd appreciate it if you close out the question. However, if there are still open issues, please let me know and I'll try to continue helping. Thanks, Joe
I recommend that post https:#a39230433 be the Accepted Solution and post https:#a40230116 be an Assisted Solution. After I made those posts with the AutoHotkey (AHK) script and instructions on calling it from Task Scheduler, the asker said, "Thanks Joe. It works perfectly although I'd rather use vbscript. Until I can figure out what's wrong with GetObject(), though, I'll use your solution."
He apparently continued to use my solution, because in a subsequent post he said, "While the AHK script works perfectly when I'm logged on and working on the desktop, it doesn't seem to work when I lock it at night (still logged in, though)."
I solved that problem for him in my post https:#a40234244 (which could also be an Assisted Solution), after which he said, "That worked! Thanks Joe."
We exchanged a few posts after that and then he abandoned the question, never closing it. Regards, Joe
He apparently continued to use my solution, because in a subsequent post he said, "While the AHK script works perfectly when I'm logged on and working on the desktop, it doesn't seem to work when I lock it at night (still logged in, though)."
I solved that problem for him in my post https:#a40234244 (which could also be an Assisted Solution), after which he said, "That worked! Thanks Joe."
We exchanged a few posts after that and then he abandoned the question, never closing it. Regards, Joe
Thank you, Mr. Wolfe — I appreciate it!
Have you tried using Task Kill?
Taskkill.exe /f /im outlook.exe