[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Run a VB Script as a different domain account

Posted on 2010-03-26
21
Medium Priority
?
817 Views
Last Modified: 2012-05-09
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?
0
Comment
Question by:seaninman
  • 7
  • 7
  • 3
  • +2
21 Comments
 
LVL 3

Expert Comment

by:treepio
ID: 28692723
0
 
LVL 3

Expert Comment

by:treepio
ID: 28693055
So:
psexec \\computername -u username -p password filetorun.exe
0
 
LVL 15

Expert Comment

by:sr75
ID: 28694757
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 1

Expert Comment

by:pirizarry
ID: 28752349
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
 
LVL 1

Expert Comment

by:pirizarry
ID: 28752450
I shoud have read sr75 more closely.  I now see that the answer I just gave was alread given by him.  Sorry.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 1000 total points
ID: 28757102
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
 
LVL 4

Author Comment

by:seaninman
ID: 28984822
So Rob Sampson, would I just add this script to the script that i'm trying to run as an administrator?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 29022195
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
 
LVL 4

Author Comment

by:seaninman
ID: 29105840
So to prompt for the password do you just leave the password blank?
0
 
LVL 15

Expert Comment

by:sr75
ID: 29111973
Nope, change this line:

strPassword = "password"


to this line:

strPassword = InputBox("Please enter your password:")
0
 
LVL 4

Author Comment

by:seaninman
ID: 29112972
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
 
LVL 15

Expert Comment

by:sr75
ID: 29114594
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
 
LVL 4

Author Comment

by:seaninman
ID: 29115142
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
 
LVL 15

Expert Comment

by:sr75
ID: 29116202
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
 
LVL 4

Author Comment

by:seaninman
ID: 29116966
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
 
LVL 15

Expert Comment

by:sr75
ID: 29117605
sorry I had a typo.  I dropped the leading "
0
 
LVL 4

Author Comment

by:seaninman
ID: 29118080
get error ActiveX component cant create object: 'ScriptPW.Password'
0
 
LVL 15

Expert Comment

by:sr75
ID: 29118397
you are not running the script as cscript
0
 
LVL 4

Author Comment

by:seaninman
ID: 29118714
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
 
LVL 15

Assisted Solution

by:sr75
sr75 earned 1000 total points
ID: 29119217
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 29139333
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question