vbscriptcmdshellexecutewsh

How to Close the CMD prompt window after successfull execution of VBScript


I am invoking a VBScript from another to run as administrator. following is the Invoker VBScript code

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cscript", "C:\Temp\XYZ.vbs", "", "runas", 0
Wscript.Quit 1

Following is the XYZ.vbs code

On Error Resume Next 

Dim strComputerRole, strDomain, strComputer, strText, strText2
Dim arrServiceList, strNextLine
Dim objFile, objFile2, strFile, strSysDrive, strTempDir, strSQLcommand

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = wscript.createObject("Wscript.Shell")
Set colProcEnvVars = objShell.Environment("Process")
Set colSystemEnvVars = objShell.Environment("System")

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
strSysDrive = colProcEnvVars("systemdrive")
strTempDir = colProcEnvVars("SY0")

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8


If strSysDrive = "" Then
    strSysDrive = "C:"
End If

If strTempDir = "" Then
    strTempDir = strSysDrive & "\Temp"
End If

If Not objFSO.FolderExists(strTempDir) Then
    objFSO.CreateFolder(strTempDir)
End If  
strFile = strSysDrive & "\temp\XYZ.txt"
strSQLcommand="osql -o "& strFile & " -d HOST -E -Q ""************Select Query******** """
Set objExecObject = objShell.Exec(strSQLcommand)

Following OSQL cmd executed successfully. But after execution, i am getting this CMD prompt window opened. Is there a way to close the command prompt after successful execution ? enter image description here


Solution

  • You specified /K after cmd.exe which means "Execute command and keep command window open". Use /C instead, which means "Execute command and close command window then".

    If you execute "cmd.exe /?" you get a list of all parameters and their descriptions.