Avatar of celtician
celtician
Flag for American Samoa asked on

Closing cmd window after script execution

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

VB ScriptWindows 2000

Avatar of undefined
Last Comment
zalazar

8/22/2022 - Mon
Surone1

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.
ASKER CERTIFIED SOLUTION
Lee W, MVP

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
zalazar

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy