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)
Related
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
I've used the below script a couple of times to kill processes in a vbscript without any issues.
This time I'm trying to kill explorer.exe. Only issue is after I use the script to kill explorer.exe within 2 seconds explorer process restores.
I don't understand ? because if I manually kill explorer.exe with Task Manager, the process is killed until I start the process again. So whats the issue with the below script?
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'explorer.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()
Next
WScript.Quit
A way :
Set oCMD = CreateObject("WScript.Shell")
oCMD.Run "taskkill /f /im explorer.exe",0,True
You can try like this :
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'explorer.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
Or like this way :
Option Explicit
Dim Process
Process = "Explorer.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
'****************************************************
How to terminate a VBS File using Vb script.. I tried this code and it is not working,
Call StopProcessVBS(strComputer,strProcess)
Function StopProcessVBS (strComputerArg,strProcessArg)
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
For Each objItem in colItems
If objItem.CommandLine = strProcessArg Then
**objItem.CommandLine.Terminate()**
End If
Next
Set WshShell = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
End Function
Finally Worked, Tried the below code
strComputer = "."
Call StopProcessVBS(strComputer,strProcess)
Function StopProcessVBS (strComputerArg,strProcessArg)
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
For Each objItem in colItems
If Instr (1,Replace(objItem.CommandLine,"""",""),strProcessArg) Then
objItem.Terminate()
End If
Next
Set WshShell = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
End Function
So I have the following scipt that I run when a user logs off using the logoff script section in Group Policy (I would like to run a check to see if it has already run before on the particular computer. If it has run before I would like the script to exit. If it hasn't then I want it to run the script and mark itself as "already been run". How can I do that?):
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = TRUE")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_ Next
I figured it out. Here's the revised script:
Option Explicit
Dim oShell,strComputer,objWMIService,colInstalledPrinters,objPPrinter
Private Function KeyExists (keyName)
Dim bKey
On Error Resume Next
bKey = oShell.RegRead(keyName)
If TypeName (bKey) = "Empty" Then
KeyExists = False
Else
KeyExists = True
End If
End Function
Set oShell = CreateObject("Wscript.Shell")
If keyExists("HKEY_LOCAL_MACHINE\Software\CRusse\RemovePrinters") Then
wscript.quit
Else
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = TRUE")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next
oshell.RegWrite "HKEY_LOCAL_MACHINE\Software\CRusse\RemovePrinters", 1, "REG_SZ"
End If
Set oShell = Nothing
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