Link to home
Start Free TrialLog in
Avatar of seaninman
seaninmanFlag for United States of America

asked on

Run a VB Script as a different domain account

I have some VB scripts that are on my network that I need to run, but because i dont have admin rights with the account that im logged into my workstation with, the scripts will not run.  

Is there a way other than using the runas cmd to have these scripts ask me for the user id and password of the account i wish to run it with?
Avatar of treepio
treepio

So:
psexec \\computername -u username -p password filetorun.exe
as stated psexec will do it.  Same with the RunAs command and if you configure a scheduled task and run that manually.  Other than that, I don't believe there are any more options.
There is another way to do a quasi-RunAs.  Set up the script to run as a scheduled task.  One of the steps asks for username/password credentials for running the script.  It's not elegant or contained directly in the script, but it (usually) works.
I shoud have read sr75 more closely.  I now see that the answer I just gave was alread given by him.  Sorry.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of seaninman

ASKER

So Rob Sampson, would I just add this script to the script that i'm trying to run as an administrator?
No, you should be able to paste, for the most part, your script in this section


Else
        ' On the second run, the user account will now be the required one, and the rest of
        ' the script can run
        ' Enter your code here
        MsgBox "Done"
End If

where it says Enter your code here......you may need to change a declaration or two, but as long as your script doesn't accept arguments, it should work fine.  If your script *does* accept arguments, how many? We can make my code continue to pass those.

Regards,

Rob.
So to prompt for the password do you just leave the password blank?
Nope, change this line:

strPassword = "password"


to this line:

strPassword = InputBox("Please enter your password:")
Is it suppose to ask for the password twice?  Thats what its doing but it works.  Also is it possible to Astrix the password out?
You can, sortof.  but the script needs to be run by cscript and not wscript.


Here is how to mask the password:

Set objPassword = CreateObject("ScriptPW.Password")
WScript.StdOut.Write "Please enter your password:"

strPassword = objPassword.GetPassword()
Wscript.Echo
Wscript.Echo "Your password is: " & strPassword

So replace

strPassword = InputBox("Please enter your password:")

With what you provided above?

Or do I need to modify this line to?

strCommand = strPSExec & " -accepteula -e -i -u " & strUser & " -p " & strPassword & " \\" & objNetwork.ComputerName & " wscript.exe """ & WScript.ScriptFullName & """"
what I provided was how to mask it and it only works if you run the script using cscript.exe and not wscript.exe.

so instead of this line:

strPassword = InputBox("Please enter your password:")

use these lines:

set objPassword = CreateObject(ScriptPW.Password")
wscript.StdOut.Write "Please enter your password:"
strPassword = objPassword.GetPassword()



So I have this... and it gives an error on line 6 Char 51 "Unterminated String Constant"
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain
strUser = strDomain & "\adminuser"
set objPassword = CreateObject(ScriptPW.Password")
wscript.StdOut.Write "Please enter your password:"
strPassword = objPassword.GetPassword()


' Check if the user name currently being used to run this script
' is the same as those specified above
If LCase(objNetwork.UserDomain & "\" & objNetwork.UserName) <> LCase(strUser) Then
        ' If not, use PSExec to launch the script again with the above credentials
        strPSExec = "\\server name\share\PsTools\psexec.exe"
        strPSExec = objFSO.GetFile(strPSExec).ShortPath
        strCommand = strPSExec & " -accepteula -e -i -u " & strUser & " -p " & strPassword & " \\" & objNetwork.ComputerName & " wscript.exe """ & WScript.ScriptFullName & """"
        objShell.Run strCommand, 0, False
Else
        ' On the second run, the user account will now be the required one, and the rest of
        ' the script can run
		'Paste VBScript source code below

Open in new window

sorry I had a typo.  I dropped the leading "
get error ActiveX component cant create object: 'ScriptPW.Password'
you are not running the script as cscript
Its a huge script i guess i'll just use this then.. :)

strPassword = InputBox("Before you enter your password here, make sure no-one is looking!:)")

Do you know why it prompts me twice for a password when using the line above?  

It prompts for a password when i run it, then it pops up asking me to click "run" for PsExec & then it prompts for the password again.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, I know this is already closed, but try this. It will make the script run in cscript, and should only ask for your password once, in a masked command prompt.

Regards,

Rob.
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objNetwork = CreateObject("WScript.Network") 
strDomain = objNetwork.UserDomain 
strUser = strDomain & "\adminuser" 
 
' Check if the user name currently being used to run this script 
' is the same as those specified above 
If LCase(objNetwork.UserDomain & "\" & objNetwork.UserName) <> LCase(strUser) Then 
        ' If not, use PSExec to launch the script again with the above credentials 
        strPSExec = "\\server\share\psexec.exe" 
        strPSExec = objFSO.GetFile(strPSExec).ShortPath 
		Set objPassword = CreateObject("ScriptPW.Password") 
		WScript.StdOut.Write "Please enter your password:" 
		strPassword = objPassword.GetPassword() 
        strCommand = strPSExec & " -accepteula -e -i -u " & strUser & " -p " & strPassword & " \\" & objNetwork.ComputerName & " wscript.exe """ & WScript.ScriptFullName & """" 
        objShell.Run strCommand, 0, False 
Else 
        ' On the second run, the user account will now be the required one, and the rest of 
        ' the script can run 
        ' Enter your code here 
        MsgBox "Done" 
End If

Open in new window