[VBS] Hide Command Window when using Exec() RRS feed

  • Question

  • Hi!

    I have a problem to hide my Command Window when using Exec() in Visual Basic Script.

    I've found some solutions to hide it but, in my case, I need to Put the retourned value in a variable.

    Here is how I drop the value:

    	Set lt = oShell.Exec("cmd.exe /C echo %time:~0,2%%time:~3,2%%time:~6,2%")
    	strLT = ""
    	Do While Not lt.StdOut.AtEndOfStream
    		LocalTime = strLT & lt.StdOut.ReadLine()

    I use it like this for an autoreboot script:

    Function oneDay
    	If hhLocalTime = "12" or hhLocalTime = "16" Then
    		If jjUpDate+1 >= jjLocalDate+0 Then
    			If LocalTime >= UpTime Then
    				Dim reponse
    				Dim reboot
    				reboot = "cmd.exe /C shutdown /f /r"
    				reponse = MsgBox (oneDTxt1 & vbCrLf & oneDTxt2,vbYesNo+vbInformation)
    				Select Case reponse
    					Case vbYes
    						Dim Sh 
    						Set Sh = CreateObject("WScript.Shell")
    						SH.Run reboot, 0, True
    						Set Sh = Nothing 
    				End Select
    			End If
    		End If
    	End If
    End Function

    When the script execute the shell I have (for each Exec()) a Window who quickly appear and disappear. And the solutions i can found on the net doesn't set the value in a variable.

    So I don't know how to hide by keeping the set value and don't create or use an other file.

    I meet a lot of constraint due to my Director. Want to execute my script by Task Scheduler et want to hide the windows because users might be affraid!

    Sorry for my English, i'm french but french forums...

    Thank you in advance.


    MESLARD Benjamin

    • Moved by Bill_Stewart Friday, July 7, 2017 7:13 PM This is not "fix/debug/rewrite my script for me" forum
    Monday, June 12, 2017 8:39 AM

All replies

  • In VBS a command (Exec) does not return values it returns a process object.  You cannot hide a process when running shell exec unless the process has command line that allows it to be hidden. Only the "Run" command allows a window to be hidden.

    You can do this in PowerShell.

    VBScript is deprecated and will not be used in future versions of Windows.  Don't waste time learning VBScript.  Learn PowerShell.


    Monday, June 12, 2017 8:52 AM
  • Thanks for your answers but we have some computer under Windows XP in England and Spain, we prefer PowerShell (a lot!) but can't use it at this time.

    We're working for a general Windows 10 migration but not for soon. We have to deploy a lot of security update... by GPO et some people doesn't restart there computers.

    That's why I've to use VBScript^^

    Monday, June 12, 2017 9:06 AM
  • This is not necessary on XP and PowerShell runs fine on XP.

    Updates will not show a UI if the options are set correctly.


    Monday, June 12, 2017 9:25 AM
  • You don't help me, i just apply my director's directive :/
    Monday, June 12, 2017 9:37 AM
  • Sorry but there is no answer to your question.  The Exec method cannot hide the command Window if the command is not able to be run hidden.

    You can use the "Run" method and output to a file then read the file.


    • Edited by jrv Monday, June 12, 2017 9:41 AM
    Monday, June 12, 2017 9:39 AM