Closing cmd window after script execution

celtician
celtician used Ask the Experts™
on
The next script opens a cmd window to call an external command, and after using it the cmd window stays open. how do i close it??

Because wshShell.Quit doesn't work, it returns an error. Is there any of way of closing it?

this is the script

Option Explicit
Dim objWMIService, objProcess, objFSO, objTextFile
dim colProcess,colServices, colItems
dim objItem, objService
Dim strComputer, strProcessKill, strProcessMem
Dim memLimit, processMem
Dim mustKill
Dim WshShell
Const ForAppending = 8

Set WshShell = WScript.CreateObject("WScript.Shell") 
strComputer = "."
strProcessKill = "'DLLHOST.EXE'"
strProcessMem = "'DLLHOST'"
memLimit = 10000
mustKill = 0
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & strProcessKill )
dim intRC
For Each objProcess in colProcess    
    processMem = objProcess.WorkingSetSize/1024
                If processMem >= memLimit Then
		       mustKill = 1
                End If    
    if mustKill = 1 then		
 	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile ("c:\informe_dllhost.txt", ForAppending, True)
	objTextFile.WriteLine(now & vbnewline & " Process has been killed with " & processMem & " Kb" & vbnewline & vbNewLine)
	objTextFile.Close
	wshShell.Run "cmd /k C:\pskill.exe -accepteula dllhost.exe", 1, True	
	WScript.Quit
    end if
Next
 
WScript.Quit

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
the easiest way is creating a 2 line bat file
C:\pskill.exe -accepteula dllhost.exe
exit

Open in new window

and then run the .bat file instead of pskill directly. the exit command should now close the command window.
Technology and Business Process Advisor
Most Valuable Expert 2013
Commented:
Why open a command prompt - why not just use:
wshShell.Run "C:\pskill.exe -accepteula dllhost.exe", 1, True

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Lee is correct, there's no need to run cmd.exe just to start another exe.
But in case you're interested, the issue is in line 31:
wshShell.Run "cmd /k C:\pskill.exe -accepteula dllhost.exe", 1, True

Open in new window

     
With the "/k", you're telling cmd.exe to run the command following, and then Keep the window open.
Replace the "/k" with a "/c", and the window will Close after execution of the command.
wshShell.Run "cmd /c C:\pskill.exe -accepteula dllhost.exe", 1, True

Open in new window

Both solutions provided by Lee W. and oBda should work.
If it bothers you that you see all kind of popping command windows then also replace the "1" with 7. The Run method will then start the command window minimized and without activating it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial