Cannot close window using VBS - vbscript

I want to start a program and then close it, not kill it, with Ctrl+W to send it to the system tray. My script is:
Set wshShell = WScript.CreateObject("WScript.Shell")
exeName = "d:\MyProgram.exe"
wshShell.Run exeName
Wscript.Sleep 10000
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'MyProgram.exe'")
'Set wshShell = WScript.CreateObject( "WScript.Shell" )
For Each objProcess in colProcesses
Wscript.Sleep 3000
wshShell.AppActivate(objProcess.ProcessID)
wshShell.SendKeys "^{w}"
Next
Set wshShell = nothing
Set objWMIService = nothing
My issue is the program window is not being activated and closed. However if I run the program first manually, delete the run command (wshShell.Run exeName) in the script and then run the script the program is set Active and Ctrl+W is sent and the window is closed.
Why can't I start and close the program via the one script?
TIA

Related

WScript.Shell not opening command window

I have an executable EMC_WS.EXE and I want to run this using the command
WshShell.Run "EMC_WS.EXE", 1, False
The executable runs correctly, but the command window is not appearing. I have used mingwstudio to generate EMC_WS.EXE.
Dim pathString, UserPath
Dim position
Dim WshShell, Shell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Shell = WshShell.Environment("User")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'EMC_WS.EXE'")
For Each objProcess In colProcessList
objProcess.Terminate()
Next
WshShell.Run "EMC_WS.EXE", 1, False
Set WshShell = Nothing
WScript.Quit(0)

Close an open OneNote application using VBS

I'm trying to close an open OneNote application on a user's computer using VB Script. However, I cannot seem to get it to work. I need to close any open OneNotes before I run the rest of the VBS file. So far I've tried this, but doesn't work for OneNote.
Set oNote= CreateObject("WScript.Shell")
oNote.Exec "onenote"
oNote.Terminate
This is another code I've tried. Neither work.
Set oNote= CreateObject("onenote")
oNote.Quit
You can try like this way to kill Onenote.exe process
Option Explicit
Dim Process
Process = "Onenote.exe"
Call Kill(Process)
'****************************************************
Sub Kill(Process)
Dim Ws,Command,Execution
Set Ws = CreateObject("Wscript.Shell")
Command = "cmd /c Taskkill /F /IM "& Process &""
Execution = Ws.Run(Command,0,True)
Set Ws = Nothing
End Sub
'****************************************************
Or by this way :
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'Onenote.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate(1)
Next

Restarting Computer via VBS

I need a less dogey/more efficient way to restart my computer through VBS. The code below is an example of what I have so far. Is there a more efficient way to restart other then SendKeys method??
Option Explicit
Dim obj
set obj= creatobject("wscript.shell")
obj.run "CMD"
wscript.sleep 300
obj.SendKeys "shutdown /r"
obj.SendKeys "{ENTER}"
obj.SendKeys "exit"
obj.SendKeys "{ENTER}"
wscript.quit
Thanks
You can use winmgmts, this script takes an argument for the computer name but can easily be changed for your use
If Wscript.Arguments.Count = 0 Then
strComputer = inputbox("Enter a computer name to Restart","Enter computer name")
if strComputer = "" then wscript.quit
Else
strCOmputer = Wscript.Arguments.Item(0)
End If
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Reboot()
Next
it will work
Set OpSysSet = GetObject("winmgmts:{authenticationlevel=Pkt," _
& "(Shutdown)}").ExecQuery("select * from Win32_OperatingSystem where "_
& "Primary=true")
for each OpSys in OpSysSet
retVal = OpSys.Reboot()
next

Close an application using VBScript

I was trying to open and close an application.
I tried like this:
Dim App1
Set App1 = CreateObject("WScript.Shell")
App1.Run("firefox")
App1.Quit
Firefox will open, but it will not close.
Error message:
object doesn't support this property or method
I referred InDesign Scripting how to quit application (not document)
Please tell me the procedure to close the application.
If you want to be able to terminate a process that way you need to use the Exec method instead of the Run method.
Set ff = CreateObject("WScript.Shell").Exec("firefox")
'you do stuff
ff.Terminate
I observed a few ways:
taskkill - Worked only if I try to kill the same process, as I run.
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run """C:\My_Scripts\ddl.exe"" -p1 -c"
'some code
oShell.Run "taskkill /f /im ddl.exe", , True
SendKeys - Works for all app, if the window name is known.
Dim oShell : Set oShell = CreateObject("WScript.Shell")
filename = "C:\Some_file.txt - Notepad"
act = oShell.AppActivate(fileName)
oShell.SendKeys "% C"
WMI object - 2 ways above worked good for me so I didn't try it.
You can find an example here:
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Chrome.exe'")
Set oShell = CreateObject("WScript.Shell")
For Each objProcess in colProcessList
oShell.Run "taskkill /im chrome.exe", , True
Next
Here is an alternative VBScript implementation:
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
Firefox is not a COM object. There is no Firefox COM object. Firefox does not want you to use COM. Or NET. Or Microsoft. That is why you could not create a Firefox object, so you created a WScript.Shell object instead.
WScript.Shell does not have a quit method. If it did, it wouldn't help kill Firefox.
This is an example of using WMI from VBS to start, then kill a process like Firefox.
Good work for this example:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run """C:\My_Scripts\ddl.exe"" -p1 -c"
'some code
oShell.Run "taskkill /f /im ddl.exe", , True

How to terminate process using VBScript

I have this VBScript code to terminate one process
Const strComputer = "."
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
It works fine with some processes, but when it comes to any process runs under SYSTEM, it can't stop it.
Is there is anything I need to add to kill the process under SYSTEM?
The way I have gotten this to work in the past is by using PsKill from Microsoft's SysInternals. PsKill can terminate system processes and any processes that are locked.
You need to download the executable and place it in the same directory as the script or add it's path in the WshShell.Exec call. Here's your sample code changed to use PsKill.
Const strComputer = "."
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
For Each objProcess in colProcessList
WshShell.Exec "PSKill " & objProcess.ProcessId
Next
Try explicit assert debug privilege {impersonationLevel=impersonate,(debug)}:
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate,(debug)}!\\.\root\CIMV2")
Set procs = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name='SearchIndexer.exe'", , 48)
For Each proc In procs
proc.Terminate
Next

Resources