We help IT Professionals succeed at work.
Get Started

Closing cmd window after script execution

celtician
celtician asked
on
3,065 Views
Last Modified: 2015-07-07
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
Technology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013
Commented:
This problem has been solved!
Unlock 2 Answers and 4 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE