Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How excecute a command as administrator ?

Status
Not open for further replies.

hervebin

IS-IT--Management
Feb 21, 2002
35
FR
Hello

I would like to know how I can execute a command in a VBS, with the right of administrator under a user'profile ?

In fact, I would like to stop a service but I have not the right when I am a user.

Am I clear ? Do you understand me ?

Thanks

Hervé
 
Scripts run with elevated priviledges so that should not be a problem. Just verify in your GPO that you have selected to always run with elevated priviledges. Here is the code you need.

set WSHShell = wscript.createObject("wscript.shell")
Call WSHShell.Run("cmd.exe /C Net Stop " & Chr(34) & "Print Spooler" & Chr(34))

If the service name is only one word, there is no need for the &Chr(34) which simply tells the script to add quotes around the service name. For example the Messenger service would just need:

Call WSHShell.Run("cmd.exe /C Net Stop Messenger")
 
Hmmm. Are you referring to scripts that run somehow specially? Because if you just do start, run, wscript script.vbs it runs as you I thought. At least, that's what username is displayed when you view taskmgr to see who owns which processes.
 
No, I am referring to scripts run from the network via GPO.

If you need to execute them locally, then you could always script using the runas command and encrypt the script so the passwords can't be seen. You should be aware however that the encrypted scripts can be unencrypted fairly easily by anyone who really wants to see it.
 
Hello

In fact, there are some GPO on the PC's user that remove the right to the user to change or modify the services.
So I need to launch a program under the user login, to start a service.

But when I execute your command (that I have already try), I have a message which says me that I don't have the good rights.

I could use the command "runas" but I don't want that the user enter the administrator's password.
Can you show me how to crypt this password and enter this password when the "runas" ask it me ?

Hervé

PS: sorry for my english, I am French.
 
Use the runas command first, then use send keys to pass your password and hit enter.

Call WSHShell.Run("RUNAS /USER:Administrator CMD.EXE /C Net Stop Service")

WSHShell.Sendkeys "Password"
WSHShell.Sendkeys "{Enter}"

Test the script unencrypted first to make sure you have it working. When it is, you can encrypt it. Download the script encoder from
 
Thanks a lot!!

It is all good for me!!

Bye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top