We help IT Professionals succeed at work.

Need assistance in using psexec in vbscript

3,957 Views
Last Modified: 2013-11-26
I am trying to use psexec in my vbscript to run certain programs as an administrator.
Lets take cmd prompt for example. I use the psexec command to open the cmd prompt on the local machine as using admin credentials. The problem is that it opens up, but then closes right away. Here is the code I am using

objShell.Exec ("psexec.exe \\" & strComputerName & "-u " & strUsername1 & "-p " & strPassword & "cmd.exe")

Is there an argument I can use or something to do this?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
psexec.exe is presumably in your path?
If not, specify the full path:

objShell.Exec ("c:\pstools\psexec.exe \\" & strComputerName & "-u " & strUsername1 & "-p " & strPassword & "cmd.exe")

Open in new window

CERTIFIED EXPERT
Top Expert 2007

Commented:
Could also be the missing space  
objShell.Exec ("psexec.exe \\" & strComputerName & " -u " & strUsername1 & " -p " & strPassword & "cmd.exe")

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
You are missing the spaces....

Try this.

Regards,

Rob.
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPSExec = "C:\Temp\PSExec.exe"
strPSExec = objFSO.GetFile(strPSExec).ShortPath
strComputer = "compname"
strUser = "user"
strPass = "pass"
strCmd = "cmd /k " & strPSExec & " -accepteula " & " \\" & strComputer & " -u " & strUser & " -p " & strPass & " cmd.exe"
objShell.Run strCmd, 1, True

Open in new window

Author

Commented:
Thanks Rob. That worked really well!

One more thing though. Is there anyway to get the vbscript to continue going instead of waiting for psexec to be done. Also, when I run this command, it opens up the psexec cmd window, and the cmd window that psexec opened up. Is there any way I can make it so that the psexec window is hidden, or make it close when I close the cmd window?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Wow. Thats great. Thanks Rob!!! Works like a charm now!
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
No problem . Thanks for the grade.

Regards,

Rob.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions