views:

1701

answers:

3

My question is similar to Running a CMD or BAT in silent mode, but with one additional constraint.

If you use WshScript.Run in vbscript, you lose access to the standard in/error/out streams of the process. WshScript.Exec gives you access to the standard streams, but you can't hide your windows. How can you have your cake (hide the windows) and eat it too (have direct access to the console streams)?

I'm currently thinking about a C++ executable which creates a new Windows Station and Desktop, (see MSDN) and runs a specified script within that new Desktop (I'm not yet an expert on Window Stations and Desktops, so this idea may be retarded).

This idea is based loosely on Condor's USE_VISIBLE_DESKTOP feature, which, if disabled, runs Condor jobs in a non-visible Desktop. I haven't quite figured out if this requires elevated priveledge.

The tradeoff of this approach is that your script can disappear into limbo if it blocks on user input.

Does anyone have any additional ideas? Or feedback on the approach outlined above?

Edit: Also, the purpose of our script is to set up the user environment, so running as another user, or as a system scheduled task isn't really an option (unless there are clever tricks I don't know about).

A: 

I only tested this a little bit, so YMMV...

Put the following code into a .vbs file (I called mine HideWindow.vbs):

Const HIDDEN_WINDOW = 12

computer = "."
Set oWmiService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")
Set oStartup = oWmiService.Get("Win32_ProcessStartup")

Set oConfig = oStartup.SpawnInstance_
oConfig.ShowWindow = HIDDEN_WINDOW
Set oProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
ret = oProcess.Create("cmd.exe /c C:\Scripts\test.cmd", null, oConfig, pid)

Call it from a batch file or command line like this:

CSCRIPT HideWindow.vbs

Whatever you put in test.cmd will run without displaying a window. This could be improved in several ways, but especially by parameterizing the program that gets called by oProcess.Create.

aphoria
A: 

I didn't have any luck with the VBScript fragment above - the windows would still pop up. However I did find a tool called hstart, which looks like about what I need. Unfortunately it isn't open source, or free for commercial use.

A: 

Cygwin (http://www.cygwin.com/) comes with a utility named run.exe which does what you are asking for a generic process. You could use this to wrap your call to cscript. Cygwin is GNU so free for commercial or personal use.

Chris Eldredge