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!
Related
Alright so I have been searching for the answer to this, and although I found similar examples to this issue, they did not seem to work for me. I am trying to have a task scheduled to kill skype when a user logs in so that people don't have to close out every time they log on. I have to do this by writing code rather than manually because it will be a script run to set up new computers. here is my code that writes the task:
please note that the commented lines are from solutions I have tried but failed to work the way I wanted it to
Option Explicit
Dim wShell, outFile, objFSO, ret
Set wShell = CreateObject ("Wscript.Shell")
wShell.Run "N:\Internfolder\INCA_Scripts\SkypeKill.vbs"
'wShell.Run "cmd start ""N:\Internfolder\INCA_Scripts\Schedule_SkypeKill.bat"""
'wShell.Run "N:\Internfolder\INCA_Scripts\Schedule_SkypeKill.bat"
wShell.Run "SCHTASKS /create /tn ""Schedule_Kill_Skype"" /tr ""N:\Internfolder\INCA_Scripts\SkypeKill.vbs"" /sc onlogon", 0
'set wShell = Nothing
'WScript.sleep(15000)
WScript.echo "completed"
and here is the .vbs file for SkypeKill:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
dim timer
timer = 0
Do
'attempt to kill skype (lync.exe)
r = oShell.Run("taskkill /F /IM lync.exe",0, True)
'if r = 0, then skyp was opened, then killed
if r= 0 then
WScript.echo "Skype was opened, but has been termiated. Hit 'Enter' to exit."
WScript.quit
'else, we wait until it is opened for 35 seconds, and kill it if it appears
else
WScript.sleep(3000)
timer = timer + 3
if timer = 35 Then Exit Do
end if
Loop
WScript.echo "Skype kill has been attempted"
the last bit of important information is that when I schedule the task to run this script, it says that the system cannot find the file specified, however the SkypeKill.vbs file is in the specified location (N:\Internfolder\INCA_Scripts\SkypeKill.vbs), which is a bit odd.
So with this I have a couple questions. I am assuming that there is something wrong with how I am attempting to schedule the task, given that it will not show up in the task scheduler when i run the first block of code. but, neither code throws any errors. How can I get this to actually write into the task scheduler and kill skype once the user has logged in and started up? Does the fact that I am running on Windows 7 and Windows 10 matter when trying to complete this task? or is there an easier way to do this via code that I am overlooking. Please help soon! Thanks!
Thank you, However I have resolved the issue simply by including the following code before the shell functions are executed:
If WScript.Arguments.Count = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", "c:\Users\admin\Documents\selfConfigure.vbs -1", "", runas", 1
End If
this elevates my priveleges to access the drive/file paths I need to access, as I was receiving an "access denied" error in command prompt. the issue has been resolved and the task has been written to the scheduler.
I'm trying to make a vb script that will restart another vb script if it crashes.
I have searched, and searched but all I get is how to restart a program and since a vb script is a background process it doesn't work when you search in Win32_Process.
Here is my code
set Service = GetObject ("winmgmts:")
set Shell = WScript.CreateObject("WScript.Shell")
sEXEName = "Test_To_Block.vbs"
while true
bRunning = false
for each Process in Service.InstancesOf ("Win32_Process")
if Process.Name = sEXEName then
bRunning=true
msgbox("I am active")
End If
next
if bRunning=False then
msgbox("I am not active.")
Shell.Run sEXEName
end if
WScript.Sleep(100)
wend
The problem is that it never see's the file running and just opens hundreds of "Test_To_Stop.vbs"'s which resolves in me having to restart the computer.
In my opinion what should be changed is where the code is looking for.
for each Process in Service.InstancesOf ("Win32_Process")
Instead of looking in "Win32_Process" you need to look in wherever background process' run.
I am new to coding so sorry if this is a simple question.
Thank you in advance.
Regards,
A Viper
The below code restarts itself via WshShell.Exec() method and trace state of the running script via .Status property of returned object:
If Not WScript.Arguments.Named.Exists("task") Then
Do
With CreateObject("WScript.Shell").Exec("""" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""")
Do While .Status = 0
WScript.Sleep 1
Loop
End With
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Another way is to use .Run() method with third parameter set to True to wait until launched process terminated:
If Not WScript.Arguments.Named.Exists("task") Then
Do
CreateObject("WScript.Shell").Run """" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""", 1, True
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Or even simplier:
If Not WScript.Arguments.Named.Exists("task") Then
Do
CreateObject("WScript.Shell").Run """" & WScript.ScriptFullName & """ ""/task""", 1, True
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Probably because the name of the running process is 'wscript.exe' and not 'Test_To_Block.vbs'. You may be able to use the hack mentioned on this page to change the name of the process:
If you're running the scripts locally and running some regular scripts, one
common hack is just to copy and rename wscript.exe to a particular name,
such as "MyScript1.exe". Then run the script with a shortcut as
"...\MyScript1.exe MyScript1.vbs". Then the process will show up as
MyScript1.exe.
Then you can use sEXEName = "MyScript1.exe"
Note: instead of using Shell.run sExeName use Shell.run "Test_To_Block.vbs"
I have an HTA that runs a backup routine. For the backup I'm using the SyncToy command line executable, which in some instances does not properly cease.
Beacuse of this, I'm trying to kill any process with the name SyncToyCmd.exe. I make use of the window_onBeforeUnload event and call the KillSyncToy() sub from there.
The function is correctly detecting instances of the SyncToyCmd.exe, however when trying to kill the process I'm receiving an error -
Error: The system cannot find the file specified.
I'm guessing that I'm doing something wrong here and any assistance would be welcome.
Sub KillSyncToy()
Dim WMIService : Set WMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim ProcessList : Set ProcessList = WMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'SyncToyCmd.exe'")
Dim Process
For Each Process in ProcessList
'** Note that 'Shell' is a global instace of the 'WScript.Shell' object, so
'there is no need to declare it here *'
Shell.Exec "PSKill " & Process.ProcessId
Next
Set WMIService = Nothing
Set ProcessList = Nothing
End Sub
The error message means that Exec can't find pskill.exe, so the executable most likely isn't located in the %PATH% or the current working directory. You could mitigate that by specifying the full path to the executable.
However, the objects returned from querying Win32_Process have a Terminate method. I'd recommend using that instead of shelling out:
For Each Process in ProcessList
Process.Terminate
Next
I feel like this should be easy, but I haven't figured it out yet.
I have an executable that is already running when I run my vbscript. I want to find the executable and call sendkeys to give it input.
Here is what I have so far:
dim service, Process, myObject
set service = GetObject ("winmgmts:")
for each Process in Service.InstancesOf ("Win32_Process")
if Process.Name = "abc.exe" then
myObject = Process
end if
next
myObject.SendKeys "This is a test."
This doesn't work, but I think it would look similar to this. I basically just want to sendkeys to myObject.
NOTE: I do not want to run a new instance of abc.exe, I want to send input to the one that is already running
You are trying to activate a wscript submethod(sendkeys) off of a process, this does not have a sendkeys submethod. Try the "App Activate" submethod off of the wscript shell....
Set objShell = WScript.CreateObject("WScript.Shell")
Do Until Success = True
Success = objShell.AppActivate("ABC")
Wscript.Sleep 1000
Loop
objShell.SendKeys "This is a test."
Reference
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