3

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

2 Answers2

6

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.

David Gausmann
  • 1,570
  • 16
  • 20
  • 1
    I've found `/K "{my command} && exit"` useful, as a way close the window *only if* the command was successful. I.e., if `%errorlevel%` is 0 -- see https://stackoverflow.com/a/14692169/254364 – Tim Goodman May 29 '20 at 21:51
0

use below line of code to close command prompt after successful operation in it.

SystemUtil.CloseProcessByWndTitle "cmd.exe", True