Forum Discussion

pwang's avatar
pwang
Occasional Contributor
3 years ago

How could i hide the CMD window When use the Sys.OleObject("WScript.Shell").Exec(CommandLine)

I just try to use Sys.OleObject("WScript.Shell").Exec(CommandLine) to run some cmd command .

But i could see that CMD window always popup when run the cmd command.

I am not sure if there is some parameters for cmd to hide the cmd windows??

 

Thanks,Peng

  • Thanks for patiently answering my questions!! The solution works for me.

  • Kitt's avatar
    Kitt
    Regular Contributor

    Thanks for sharing. Is there a particular reason you want to minimize the cmd window, instead of just closing the window after the command is run? It would be nice to have some extra context as to what your end goal is.

     

    Either way, here's an example using a file (in this case). Any shell commands longer than one line I usually thrown in a .bat or .ps1 file, but here's how to do it... 

     

    1) Just create a .txt file with the following contents:

     

     

    if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
    net stop service
    exit

     

     

    **NOTE: if you don't want cmd window to close, remove && exit from line 1 and exit from line 3

    2) Save As stopNetService.bat and add the file to your TestComplete > Stores > Files collection.

    3) Double click the batch file to run and test that it does what you want

    4) In TestComplete, create a new function as follows:

     

     

    function stopNetService() {
      // stop net service .bat
      var batch = Sys.OleObject("WScript.Shell");  
      var stopNetService = batch.Run(Project.Path + "\\Stores\\Files\\stopNetService.bat"); 
      Log.Checkpoint("| Net Service Stopped |");
      Sys.Refresh(); 
    }

     

     

     5) Use this function stopNetService() throughout your test as you please.

    • pwang's avatar
      pwang
      Occasional Contributor

      Thanks for patiently answering my questions!! The solution works for me.

  • Kitt's avatar
    Kitt
    Regular Contributor

    Here are some TestComplete references that should help: [1] & [2].

     

    Depending on whether you are running CMD via a script file (.bar or .ps1) or direct command, you could try this implementation discussed [here].

    • pwang's avatar
      pwang
      Occasional Contributor

      Thank you for your reply, But I can still not find the solution for the problem.

       

      I don't use .bar or .ps1, My code is like this:

       

      CommandLine = 'net stop service';
      var oExec = Sys.OleObject("WScript.Shell").Exec(CommandLine);

      if (oExec.StdOut.AtEndOfStream) { ...}

       

  • Kitt's avatar
    Kitt
    Regular Contributor

    No problem pwang. And if you wouldn't mind remarking the correct comment as the solution so that it is appropriately linked from the question, it will help others find the correct source. Thx and Happy testing.