I have searched the Stack Overflow site for questions related to closing
Outlook. There were a number of hits but none seem to describe what I'm
trying to do.
The problem I'm trying to solve is how to backup the Outlook data base
automatically and unattended. Outlook needs to be closed (if it is
running) before copying the .pst files.
I found a VBScript at (www.howto-outlook.com/howto/closeoutlookscript.htm)
that seems like what I need. But I can't get it to run when initiated from
the Windows Task Scheduler.
I am running on a Windows 8 Sony laptop.
My VBScript should close Outlook prior to doing a backup of the .pst files.
The code is stored in CloseOutlookVerify.vbs.
Below is the offending code from CloseOutlookVerify.vbs:
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Outlook.exe'")
For Each objProcess in colProcessList
Set objOutlook = CreateObject("Outlook.Application")
' The above line fails with ERR = 70 - Permission denied
objOutlook.Quit
Closed = 1
Next
This script works correctly if I double-click on the .vbs file
from Windows Explorer.
It works correctly if I run it from a DOS Command Prompt window.
It fails with err = 70 when run via the Windows Task Scheduler.
So, what is different about running this script from a command prompt
vs. by the task scheduler? And how can I make it work correctly when run
by the task scheduler?
FYI - I made my living programming in C and Unix shell languages, but this
is my first exposure to VBS in the Windows environment.
Many thanks for any expertise you can provide.
I think it is because impersonationLevel has not been set. Try this:
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strSysName & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Outlook.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
Related
This question already has answers here:
How to terminate process using VBScript
(2 answers)
Closed 4 years ago.
How to close/terminate/kill an instance of a application with VBS?
I've tried the following code, suggested on several posts, and it didn´t work.
Set x = GetObject(,"xxx.Application")
x.quit
I think I used WMI at one point, and that worked OK - provided the script runs with the access rights required to kill the processes in question (in other words admin rights for system processes, user rights for user context processes - I suppose there could be NT Privileges / NT Rights (system wide privileges) and custom ACL permission settings (access control list - attached to objects) involved as well - the configuration of which might vary from site to site):
Note: the below script will kill all Notepad.exe processes! (unless access is denied)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Notepad.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
Some Links:
Close an application using VBScript (several other options)
Kill a process which says “Access denied” (auperuser - Terminate Process ACL)
Unkillable Processes
During my internet searches, I have found a script that is supposed to stop a service. The current script runs, finds the services specified in an array, but doesn't seem to stop them. When the script outputs the services' State, it's still running. Below is the script.
sComputer = "."
aTargetSvcs= Array("mysql","Apache2.4")
Set oWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set cServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service")
For Each oService In cServices
For Each sTargetSvc In aTargetSvcs
If LCase(oService.Name) = LCase(sTargetSvc) Then
If oService.State <> "Stopped" Then
oService.StopService()
Wscript.Echo oService.State
End If
End If
Next
Next
I am just testing it out with mysql and Apache2.4 services, but when this works, it will be deployed with a group policy to temporarily stop some AV services that are interfering with a domain modifier script.
The issue is likely a lack of permission in the context of the running script.
If you run the script from the command line make sure to start it through an Elevated Command Prompt, in modern Windows Operating Systems an elevated Command Prompt is denoted by the prefix Administrator: in the Window Title.
If you run the script from a Shortcut link make sure to specify Run As Administrator in the Advanced Properties screen.
currently, to improve some inefficiencies on a daily process, I am trying to write a c# Winform app that will combine a mix of user-input with VBscripts that will expedite a previously all user-input process of looking at an excel file and moving files from VSS to certain folders of certain servers.
I was hoping to get some questions answered, pointed in the right way:
Using command line or other workaround instead of manually,
1) Is it possible to log into a 2003 remote desktop with a Smartcard/pin?
2) Is it possible to run a file/start a process on the remote desktop from a command on your machine?
Thanks for the help and time
I have only experience with the second question.
You can do this with remote scripting or with utilities like SysInternals PsExec http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
here a vbscript that remotely starts a ipconfig command and redirects it to a textfile
Note that you can't start interactive processes like this, they would start but not show up
Dim sComputer 'computer name
Dim sCmdLine 'command line of the process
Dim sCurDir 'working directory of the process
Dim oProcess 'object representing the Win32_Process class
Dim oMethod 'object representing the Create method
sComputer = "." 'this is the local computer, use a pcname or ip-adress to do it remote
sCmdLine = "cmd /c ipconfig.exe > c:\ipconfig.txt"
Set oProcess = GetObject("winmgmts://" & sComputer & "/root/cimv2:Win32_Process")
Set oMethod = oProcess.Methods_("Create")
Set oInPar = oMethod.inParameters.SpawnInstance_()
oInPar.CommandLine = sCmdLine
oInPar.CurrentDirectory = sCurDir
Set oOutPar = oProcess.ExecMethod_("Create", oInPar)
If oOutPar.ReturnValue = 0 Then
WScript.Echo "Create process method completed successfully"
WScript.Echo "New Process ID is " & oOutPar.ProcessId
Else
WScript.Echo "Create process method failed"
End If
I like to end a process using VBScript.
Unfortunally I only found examples in which the authors are describing how to do it killing the process.
I like to ask for closing. So objProcess.Terminate() won't help.
I'm using Windows XP SP3 with admin rights.
Any Ideas?
Thank you!
You could try the CloseMainWindow and the Close methods on the process as described on MSDN, like:
Sub KillingMeSoftly(processName)
'partly copied from http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/processes/
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & processName & "'")
For Each objProcess in colProcessList
objProcess.CloseMainWindow
objProcess.Close
Next
End Sub
this was a bad answer
UPDATE
While searching for an answer, I just discovered that starting a script with the //T:nn option triggers the Terminate event on objects:
class Foo
sub class_terminate
msgbox "Gracefull termination"
' Put your own termination code here.
end sub
end class
dim bar
set bar = new Foo
do
loop ' makes the script run forever
Save this as c:\endless.vbs
Running this script will never trigger the termination event because it will hang in the endless loop, but if you start the script with a timeout it will; Start the script from the command prompt:
C:\>wscript endless.vbs //T:5
You'll see that after 5 seconds a messagebox with "Gracefull termination" appears.
This is usefull when you want to quit a script after a certain amount of time and run a cleanup if it was not ended by itself. I do not know if this covers the solution you are searching for.
So, finally I found a solution for my problem, but is not solved using VBS.
There is a program written to send a CTRL-BREAK to any process called "SendSignal". I had tried this before, but all my process was responding had been an error-message and it kept running.
I changed this program sending CTRL+C. After this I was able to shut my Javaw process gently.
Thanks to all, for your help!
Im wondering if its posible to detect windows CE in a windows logon script ( Scrip runs in the user account ).
I assume its posible to detect this via some checking for some file, but i was hoping for a bit "cleaner" solution.
You can use the below code to check against the Windows version in VBscript using WMI. Replace the XXXXXXXXX with the appropriate version number.
strComputer = "." 'We are using computer "here"
set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2") 'Initialize WMI object for this computer
'Displays which operating system is installed on the current computer.
set colOperatingSystems = objWMIService.ExecQuery _
("Select Caption, Version from Win32_OperatingSystem") 'Query WMI for OS Version
'Validate that OS version is valid
for each objOperatingSystem in colOperatingSystems ' Parse results
if objOperatingSystem.Version = "XXXXXXXXX" Then
'Do something here
end if
next
If you're not sure what the version is, try changing the if/then statement temporarily to
WScript.Echo objOperatingSystem.Version
and running it manually. That will output the correct version # for your system.