Jump to content
Sign in to follow this  
tiho999

hide cmd.exe window

Recommended Posts

I'm working on script that will open CMD as and administrator, afterwords, it will enter comannd to and run it from network disc that will open another program as an administrator.

Thing is, I want that all windows are hidden except last window of that specific program.
Also, dunno why but cmd won't close regardless of what I set up winclose;

Local $sUsername = "username"

Local $sPassword = "password"

Local $sDomain = "domain" & "\"

Local $RunCommand = "cmd.exe"

 

Run ('C:\Windows\System32\runas.exe /profile /user:' & $sDomain & $sUsername & ' "CMD /C \"' & $RunCommand & '\""')

WinWait ("C:\Windows\System32\runas.exe")

send ($sPassword & '{ENTER}')

sleep(1000)

send ("\\network1\App1\Run_App1.exe" & ' {ENTER}')

sleep(1000)

Winclose ("CMD /C")

 

Thank you in advance!

Share this post


Link to post
Share on other sites

@tiho999 welcome to the forum. Look at the help file for your Run command, there is a parameter to hide the window. Just realize that doing so complicates using the Send command. You might instead look at RunAs or RunAsWait, which will allow you to specify the credentials and the application you're trying to run. It, too, has the option to hide any windows. The help file has much more info and an example, but it would be something like this:

RunAsWait("Admin", "mydomain.com", "MyPassword", $RUN_LOGON_NOPROFILE, "<path to exe>", "<workingdir>", @SW_HIDE)

 


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
15 minutes ago, JLogan3o13 said:

@tiho999 welcome to the forum. Look at the help file for your Run command, there is a parameter to hide the window. Just realize that doing so complicates using the Send command. You might instead look at RunAs or RunAsWait, which will allow you to specify the credentials and the application you're trying to run. It, too, has the option to hide any windows. The help file has much more info and an example, but it would be something like this:

RunAsWait("Admin", "mydomain.com", "MyPassword", $RUN_LOGON_NOPROFILE, "<path to exe>", "<workingdir>", @SW_HIDE)

 

Hi and thank you!

 

Is possible somehow, with this mine code to implement the @SW_Hide option? 

It's fully working code but impossible to hide it, or do i really have to change the whole code?

Share this post


Link to post
Share on other sites

As I said, you cannot use Send with a hidden window. You either need to look at ControlSend in the help file, or bring up your window, send the string, then hide it, if you do not want to recode it.

However, there is a case to be made for taking 20 minutes, shortening your code measurably, and doing it all in a single line :)

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
13 minutes ago, JLogan3o13 said:

 

However, there is a case to be made for taking 20 minutes, shortening your code measurably, and doing it all in a single line :)

definitely someone with my experience, can't do that :) 

will try to play with it, something will come up eventually...

Share this post


Link to post
Share on other sites

The RunAsWait line I gave you above basically has everything you are doing in your code, you just need to plug in the values. Good luck


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
1 hour ago, JLogan3o13 said:

The RunAsWait line I gave you above basically has everything you are doing in your code, you just need to plug in the values. Good luck

imo, i need to to in one run as wait, definition of both programas that needs to be opened, both locations and all possible credentials that are needed?

it's not just plug in values :)

Share this post


Link to post
Share on other sites

@tiho999 I am confused, what two programs? When you do a RunAsWait, you remove the need to do this line as you have it above:

Run ('C:\Windows\System32\runas.exe /profile /user:' & $sDomain & $sUsername & ' "CMD /C \"' & $RunCommand & '\""')

So, based on what you have written above (unless there is more you did not include), your 11 lines of code:

Local $sUsername = "username"

Local $sPassword = "password"

Local $sDomain = "domain" & "\"

Local $RunCommand = "cmd.exe"

 

Run ('C:\Windows\System32\runas.exe /profile /user:' & $sDomain & $sUsername & ' "CMD /C \"' & $RunCommand & '\""')

WinWait ("C:\Windows\System32\runas.exe")

send ($sPassword & '{ENTER}')

sleep(1000)

send ("\\network1\App1\Run_App1.exe" & ' {ENTER}')

sleep(1000)

Winclose ("CMD /C")

...could be reduced to:

RunAsWait("username", "domain", "password", $RUN_LOGON_NOPROFILE, "\\network1\app1\Run_app1.exe", "<workingdir>", @SW_HIDE)

Or, if you do not want to embed username and password in the script:

$username = InputBox("Input UserName", "Please type in UserName")
$password = InputBox("Password", "Please type in Password", "", "*")

RunAsWait($username, "mydomain.com", $password, $RUN_LOGON_NOPROFILE, "\\network1\app1\Run_app1.exe", "<workingdir>", @SW_HIDE)

 

Again, if it works for you as written great. Just pointing out a better way to do it, which will be much easier to maintain going forward. :)


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites

Maybe I was't good with explenation. 

So. I have one specific application.

It must be runed as admin by default with admin's user and password. User must not know that he is using application on that way so idea was to first login in CMD as admin, then inside of CMD run the script that will open that specific program like admin and in all that 3-4 seconds of work, on user's desktop there is no displayed that wonderwork. :)

So, default variables are; 

Local $sUsername = "username"

Local $sPassword = "password"

Local $sDomain = "domain" & "\"

Local $RunCommand = "cmd.exe"

cmd path should be;

C:\Windows\System32\runas.exe

path of that application is;

\\network1\app1\Run_app1.exe

Mine explanation of my code will be;

Local $sUsername = "username"

Local $sPassword = "password"

Local $sDomain = "domain" & "\"

Local $RunCommand = "cmd.exe"

 
 
Run ('C:\Windows\System32\runas.exe /profile /user:' & $sDomain & $sUsername & ' "CMD /C \"' & $RunCommand & '\""') - on $sDomain run cmd.exe as administrator with $sUsername

WinWait ("C:\Windows\System32\runas.exe") - cmd window is open until pass is inserted

send ($sPassword & '{ENTER}') - pass is inserted

sleep(1000) - wait 1 second

send ("\\network1\App1\Run_App1.exe" & ' {ENTER}') - input the path of program and open it 

sleep(1000) - wait 1 second

Winclose ("CMD /C") - close whole cmd window (currently it does not close)

This is what AutoIT should do automatically.

 

 

Share this post


Link to post
Share on other sites

And my response (a couple of times now) is that you can do that in a single line. RunAs or RunAsWait in AutoIt do precisely the same thing you are doing with your call to runas.exe, so you don't need it. Have you tried what I have suggested above?


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...