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?
LVL 4
seaninmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

treepioCommented:
0
treepioCommented:
So:
psexec \\computername -u username -p password filetorun.exe
0
sr75Commented:
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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

pirizarryCommented:
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.
0
pirizarryCommented:
I shoud have read sr75 more closely.  I now see that the answer I just gave was alread given by him.  Sorry.
0
RobSampsonCommented:
Hi, here is a self-calling script that uses PSExec to re-launch itself with the required credentials.  You could have it prompt for the password if you don't want to put it in the script.

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain
strUser = strDomain & "\adminuser"
strPassword = "password"

' 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
	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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
seaninmanAuthor Commented:
So Rob Sampson, would I just add this script to the script that i'm trying to run as an administrator?
0
RobSampsonCommented:
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.
0
seaninmanAuthor Commented:
So to prompt for the password do you just leave the password blank?
0
sr75Commented:
Nope, change this line:

strPassword = "password"


to this line:

strPassword = InputBox("Please enter your password:")
0
seaninmanAuthor Commented:
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?
0
sr75Commented:
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

0
seaninmanAuthor Commented:
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 & """"
0
sr75Commented:
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()



0
seaninmanAuthor Commented:
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

0
sr75Commented:
sorry I had a typo.  I dropped the leading "
0
seaninmanAuthor Commented:
get error ActiveX component cant create object: 'ScriptPW.Password'
0
sr75Commented:
you are not running the script as cscript
0
seaninmanAuthor Commented:
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.
0
sr75Commented:
that is because it relaunches itself under psexec.

Move the strPassword line to the one just above the strCommand line.  Then it should only ask you once.
0
RobSampsonCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.