RepackagingScripting

Run Console Applications & Batch Files, without any Window, in the background

Many times systems engineers come across the need to suppress the console window of a cmd prompt while setting up various batch and script automations to be run in silent mode.

Below is the list of various options available to control the cmd console window and you need to choose the option as per your requirements:

  1.  Hidden Start (a complete solution to control console windows and also supports x64bit)
  2.  Start (helps control spawned process console)
  3.  PowerShell –WindowStyle Hidden
  4.  WshShell.Run WindowStyle = 0
  5.  Javaw.exe is a console/window less Java.exe that runs Java programs as hidden process
  6.  

—————————
Hidden Start v3.2  (64-bit)
—————————
Run from command line:

hstart64.exe /[PARAMETERS]  /[PRIORITY]  [COMMAND LINES]

PARAMETERS:

/NOCONSOLE – start console applications without any windows

/ELEVATED or /RUNAS – start applications in elevated mode (Vista)

/NONELEVATED – start applications in non-elevated mode (Vista)

/UAC – run Hstart executable in elevated mode (Vista)

/SHELL – run apps, open docs and URLs in the Windows Shell

/D=PATH – set the starting directory for executed applications

/DELAY=N – delay N seconds before command execution

/WAIT – wait for the application to terminate (run synchronously)

/WAITINPUT – wait for the user input to set the priority class

/MSG=TEXT – show the message after command execution

/TITLE=TEXT – set the title of the message

/ERRMSG=TEXT – show the error message if an error detected

/ERRTITLE=TEXT – set the title of the error message

/SILENT – silent mode, no error messages will be displayed

/TEST – test mode, see what parameters are passed to Hstart

PRIORITY classes:

/[HIGH | ABOVENORMAL | NORMAL | BELOWNORMAL | IDLE]

Don’t forget about quotes when using complex COMMAND LINES:

hstart64.exe /NOCONSOLE /D="C:Dir 1"  ""C:Dir 2test.bat" -param"

—————————
OK  
—————————

 

 

C:>start /?
Starts a separate window to run a specified program or command.

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/AFFINITY <hex affinity>] [/WAIT] [/B] [command/program]
      [parameters]

    "title"     Title to display in window title bar.
    path        Starting directory.
    B           Start application without creating a new window. The
                application has ^C handling ignored. Unless the application
                enables ^C processing, ^Break is the only way to interrupt
                the application.
    I           The new environment will be the original environment passed
                to the cmd.exe and not the current environment.
    MIN         Start window minimized.
    MAX         Start window maximized.
    SEPARATE    Start 16-bit Windows program in separate memory space.
    SHARED      Start 16-bit Windows program in shared memory space.
    LOW         Start application in the IDLE priority class.
    NORMAL      Start application in the NORMAL priority class.
    HIGH        Start application in the HIGH priority class.
    REALTIME    Start application in the REALTIME priority class.
    ABOVENORMAL Start application in the ABOVENORMAL priority class.
    BELOWNORMAL Start application in the BELOWNORMAL priority class.
    AFFINITY    The new application will have the specified processor
                affinity mask, expressed as a hexadecimal number.
    WAIT        Start application and wait for it to terminate.
    command/program
                If it is an internal cmd command or a batch file then
                the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command
                has been run.

                If it is not an internal cmd command or batch file then
                it is a program and will run as either a windowed application or a console application.

    parameters  These are the parameters passed to the command/program.

NOTE: The SEPARATE and SHARED options are not supported on 64-bit platforms.

If Command Extensions are enabled, external command invocation
through the command line or the START command changes as follows:

non-executable files may be invoked through their file association just
    by typing the name of the file as a command.  (e.g.  WORD.DOC would
    launch the application associated with the .DOC file extension).
    See the ASSOC and FTYPE commands for how to create these
    associations from within a command script.

When executing an application that is a 32-bit GUI application, CMD.EXE
    does not wait for the application to terminate before returning to
    the command prompt.  This new behavior does NOT occur if executing
    within a command script.

When executing a command line whose first token is the string "CMD "
    without an extension or path qualifier, then "CMD" is replaced with
    the value of the COMSPEC variable.  This prevents picking up CMD.EXE
    from the current directory.

When executing a command line whose first token does NOT contain an
    extension, then CMD.EXE uses the value of the PATHEXT
    environment variable to determine which extensions to look for
    and in what order.  The default value for the PATHEXT variable
    is:

        .COM;.EXE;.BAT;.CMD

    Notice the syntax is the same as the PATH variable, with
    semicolons separating the different elements.

When searching for an executable, if there is no match on any extension,
then looks to see if the name matches a directory name.  If it does, the
START command launches the Explorer on that path.  If done from the
command line, it is the equivalent to doing a CD /D to that path.

C:>

 

 

 

C:>powershell /?

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { – | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

-PSConsoleFile
    Loads the specified Windows PowerShell console file. To create a console
    file, use Export-Console in Windows PowerShell.

-Version
    Starts the specified version of Windows PowerShell.

-NoLogo
    Hides the copyright banner at startup.

-NoExit
    Does not exit after running startup commands.

-Sta
    Start the shell using a single-threaded apartment.

-NoProfile
    Does not use the user profile.

-NonInteractive
    Does not present an interactive prompt to the user.

-InputFormat
    Describes the format of data sent to Windows PowerShell. Valid values are
    "Text" (text strings) or "XML" (serialized CLIXML format).

-OutputFormat
    Determines how output from Windows PowerShell is formatted. Valid values
    are "Text" (text strings) or "XML" (serialized CLIXML format).

-WindowStyle
    Sets the window style to Normal, Minimized, Maximized or Hidden.

-EncodedCommand
    Accepts a base-64-encoded string version of a command. Use this parameter
    to submit commands to Windows PowerShell that require complex quotation
    marks or curly braces.

-File
    Execute a script file.

-ExecutionPolicy
    Sets the default execution policy for the session.

-Command
    Executes the specified commands (and any parameters) as though they were
    typed at the Windows PowerShell command prompt, and then exits, unless
    NoExit is specified. The value of Command can be "-", a string. or a
    script block.

    If the value of Command is "-", the command text is read from standard
    input.

    If the value of Command is a script block, the script block must be enclosed
    in braces ({}). You can specify a script block only when running PowerShell.exe
    in Windows PowerShell. The results of the script block are returned to the
    parent shell as deserialized XML objects, not live objects.

    If the value of Command is a string, Command must be the last parameter
    in the command , because any characters typed after the command are
    interpreted as the command arguments.

    To write a string that runs a Windows PowerShell command, use the format:
        &
quot;& {<command>}"
    where the quotation marks indicate a string and the invoke operator (&)
    causes the command to be executed.

-Help, -?, /?
    Shows this message. If you are typing a PowerShell.exe command in Windows
    PowerShell, prepend the command parameters with a hyphen (-), not a forward
    slash (/). You can use either a hyphen or forward slash in Cmd.exe.

EXAMPLES
    PowerShell -PSConsoleFile SqlSnapIn.Psc1
    PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML
    PowerShell -Command {Get-EventLog -LogName security}
    PowerShell -Command "& {Get-EventLog -LogName security}"

    # To use the -EncodedCommand parameter:
    $command = ‘dir "c:program files" ‘
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    powershell.exe -encodedCommand $encodedCommand

C:>

 

InvokeHiddenProcess.vbs

C:>type InvokeHiddenProcess.vbs

Set WshShell = WScript.CreateObject("WScript.Shell")
cmd = "cmd /k Test.bat"
Return = WshShell.Run(cmd, 0, True)
set WshShell = Nothing

C:>

C:>type Test.bat
echo "helloo" > testing.txt

C:>type testing.txt
"helloo"

C:>

 

Javaw.exe

    javaw [ options ] class [ argument … ]
    javaw [ options ] -jar file.jar [ argument … ]

options
       Command-line options.
class
       Name of the class to be invoked.
file.jar
       Name of the jar file to be invoked. Used only with -jar.
argument
       Argument passed to the main function.

For more detailed arguments list refer to Sun Java’s Javaw – the Java application launcher.

Advertisements