We help IT Professionals succeed at work.

Commands Being Run via VBS Script. How to prevent cmd windows?

FIM2003
FIM2003 asked
on
I am making a VBS script that will run three commands to open ports necessary for Remote Assistance. When this VBS script is run, three cmd windows popup for a few seconds and disappear. Is there any way to prevent the window from popping up?
set oShell  = WScript.CreateObject("WScript.shell")
 
oShell.run "netsh firewall add allowedprogram  c:\windows\system32\sessmgr.exe 
 
sessmgr ENABLE"
 
oShell.run "netsh firewall add allowedprogram 
 
c:\windows\PCHEALTH\HELPCTR\Binaries\helpsvc.exe helpsvc ENABLE"
 
oShell.run "netsh firewall add portopening TCP 135 RemoteAssistance"

Open in new window

Comment
Watch Question

try using oshell.exec
or oshell.run fith flag 2 which will minimize window

Author

Commented:
I changed it to Exec and include the cmd.exe /k as you and the article specified and three cmd windows still popup for a few seconds.
set oShell  = WScript.CreateObject("WScript.shell")
 
oShell.Exec "cmd.exe /k netsh firewall add allowedprogram  
 
c:\windows\system32\sessmgr.exe sessmgr ENABLE > testing.txt"
 
oShell.Exec "cmd.exe /k netsh firewall add allowedprogram 
 
c:\windows\PCHEALTH\HELPCTR\Binaries\helpsvc.exe helpsvc ENABLE"
 
oShell.Exec "cmd.exe /k netsh firewall add portopening TCP 135 RemoteAssistance"

Open in new window

They will popup because you lounch cmd.exe
in command oshell.Exec "cmd.exe /k"
try without cmd.exe as you do not need  reference to command prompt

Author

Commented:
Oh. I have removed the "cmd.exe /k" and the windows still come up.
set oShell  = WScript.CreateObject("WScript.shell")
 
oShell.Exec "netsh firewall add allowedprogram  c:\windows\system32\sessmgr.exe 
 
sessmgr ENABLE > testing.txt"
 
oShell.Exec "netsh firewall add allowedprogram 
 
c:\windows\PCHEALTH\HELPCTR\Binaries\helpsvc.exe helpsvc ENABLE"
 
oShell.Exec "netsh firewall add portopening TCP 135 RemoteAssistance"

Open in new window

there is a possibility that netsh will be opening comamnd promp window how about minimizing them if you would use

oshell.run (your command, 2)
set oShell  = WScript.CreateObject("WScript.shell")
 
oShell.run "netsh firewall add allowedprogram  c:\windows\system32\sessmgr.exe sessmgr ENABLE", 2
oShell.run "netsh firewall add allowedprogram c:\windows\PCHEALTH\HELPCTR\Binaries\helpsvc.exe helpsvc ENABLe", 2
 
oShell.run "netsh firewall add portopening TCP 135 RemoteAssistance", 2






it will minimize windows

Author

Commented:
That will do the trick. Thanks!
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi guys,

The Exec method of the Shell object can not force hidden windows. It's advantage is that you can read the output from a command prompt.  The Run method, as stated, does have a parameter that allows the window to be displayed in any form, including hidden.  It actually has three parameters:
http://msdn2.microsoft.com/en-us/library/d5fk67ky.aspx

object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

You can see in the link above the 10 window styles that can be used, including a zero, which puts the application that is to be run in a hidden state.

The third parameter tells the rest of the script to wait until the command being run has finished, before it continues executing more code.

So, you can run the following command hidden:
Set oShell  = CreateObject("WScript.shell")
strCommand = "cmd /c netsh firewall add allowedprogram  c:\windows\system32\sessmgr.exe sessmgr ENABLE > testing.txt"
oShell strCommand, 1, True

to have the cmd program run in a normal window style.  If you use
oShell strCommand, 0, True

the cmd program runs hidden.

Now, for testing, to see cmd window output, you can use:
Set oShell  = CreateObject("WScript.shell")
strCommand = "cmd /k netsh firewall add allowedprogram  c:\windows\system32\sessmgr.exe sessmgr ENABLE > testing.txt"
oShell strCommand, 1, True

Where I've put cmd /k, not cmd /c.  This is usually used for testing, so the command window stays open when the command has finished.  You should notice that you must *not* use cmd /k with a zero window style, because then you'll have an open command prompt, which is hidden, and you have to kill it via the task manager.

Hope that helps,

Regards.

Rob.

Author

Commented:
Thanks, Rob. I'll give that a try!
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
No worries....looks like I was too late, but oh well!  It's just more info....

Regards,

Rob.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.