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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incorporating a RunAs command 1

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Hi

In order to deny our users local admin rights while still giving us an easy opportunity to run stuff with admin rights I'm writing a RunAs script (mainly for the benefit of my change-resistant colleagues!) to open up a Windows-Explore-like File Manager under Admin privileges.

Having found code that will process the built-in RunAs CMD command within the same window as the cscript-launched VBScript the 'only' remaining issue is that nothing seems to be happening at the most vital moment.

Here's the code (you'll have to change the domain name (duh!) and the names of the executables, specified in the constants conApp1 and conApp2, to something that exists on your PC). The problem occurs in the RunCmd function (line 92). The GetPassword routine has been abandoned because the RunAs command won't accept a password given to it as an argument:
Code:
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' LocalRunAsAdmin.vbs
'
' v1.00  -  First version. To run a File Manager under admin privileges when the 
'		    local console is being run under an ordinary user's account.
' v1.01  -  Parse the password without echoing it to screen
'			([URL unfurl="true"]http://blogs.technet.com/heyscriptingguy/archive/2005/02/04/how-can-i-mask-passwords-using-an-inputbox.aspx)[/URL]
'			The Functions Run and RunCMD are from
'			[URL unfurl="true"]http://www.source-code.biz/snippets/vbscript/3.htm[/URL]
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'on error resume next
CheckEngine
Dim objWshShell : Set objWshShell = CreateObject("WScript.Shell")
Dim strVersion
    strVersion = "1.01"
Dim strAppName
	strAppName = "Run As Admin"

'CONST
Const conApp1 = "C:\Program Files\FreeCommander\FreeCommander.exe"
Const conApp2 = "C:\Program Files\2xExplorer\2xExplorer.exe"

'GLOBAL VARIABLES
Dim strApp
	
Dim strAccountName : strAccountName = InputBox("Enter the name of an admin Domain account name",strAppName & " v" & strVersion,"Administrator")
'MsgBox strAccountName
'GetPassword
CheckExecutable
'objWshShell.Run "runas /profile /user:YourDomainName\" & strAccountName & " " & strApp
Dim strCMD : strCMD = """runas /profile /user:YourDomainName\" & strAccountName & " """ & strApp & """"""

wscript.echo "Run(" & strCMD & ")"
'Run(strCMD) ' Doesn't work as RunAs is an internal command interpreter command.

wscript.echo "RunCMD(" & strCMD & ")"
RunCMD(strCMD)

'-------------------------------------------------------------------------------
Sub CheckEngine
    'wscript.echo " WScript.FullName = " & WScript.FullName
    Dim pcEngine : pcEngine = LCase(Mid(WScript.FullName, InstrRev(WScript.FullName,"\")+1))
    If Not pcEngine="cscript.exe" Then
        'wscript.echo " Engine = " & pcengine
        Dim objWshShell : Set objWshShell = CreateObject("WScript.Shell")
		objWshShell.Run "CSCRIPT.EXE /nologo """ & WScript.ScriptFullName & """"
        WScript.Quit
    End If
End Sub
'-------------------------------------------------------------------------------
Sub GetPassword
	Dim objPassword : Set objPassword = CreateObject("ScriptPW.Password") 
	WScript.StdOut.Write "Please enter your password:" 

	Dim strPassword : strPassword = objPassword.GetPassword() 
	'Wscript.Echo
	'Wscript.Echo "Your password is: " & strPassword
End Sub
'-------------------------------------------------------------------------------
Sub CheckExecutable
	Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
	if objFSO.FileExists(conApp1) then
		strApp = conApp1
		wscript.echo "Found " & strApp
	Else
		strApp = conApp2
		wscript.echo "Found " & strApp
	End If
End Sub
'-------------------------------------------------------------------------------
Function Run (ByVal cmd)
	Dim sh: Set sh = CreateObject("WScript.Shell")
	Dim wsx: Set wsx = Sh.Exec(cmd)
	If wsx.ProcessID = 0 And wsx.Status = 1 Then
	 ' (The Win98 version of VBScript does not detect WshShell.Exec errors)
		Err.Raise vbObjectError,,"WshShell.Exec failed."
	End If
	Do
		Dim Status: Status = wsx.Status
		WScript.StdOut.Write wsx.StdOut.ReadAll()
		WScript.StdErr.Write wsx.StdErr.ReadAll()
		If Status <> 0 Then Exit Do
		WScript.Sleep 10
	Loop
	Run = wsx.ExitCode
End Function
'-------------------------------------------------------------------------------
' Runs an internal command interpreter command.
Function RunCmd (ByVal cmd)
	wscript.echo "We're in RunCMD"
	RunCmd = Run("%ComSpec% /k " & cmd)
End Function
'-------------------------------------------------------------------------------
Sub QuitOut
    Set objWshShell = Nothing
End Sub
And yes, I know I've not done all the [tt]Set objName = Nothing[/tt] yet...
:)

Figuring that perhaps the prompt for the password isn't being echoed to screen I've tried entering the password but nothing happens. When I press Ctrl-C the password is then echoed to screen as if it was a command:
Code:
P:\Scripts>Password
'Password' is not recognized as an internal or external command,
operable program or batch file.
If I change /k as the Command Interpreter argument to /c the script just ends:
Code:
P:\Scripts>cscript //nologo LocalRunAsAdmin_v1.01.vbs
Found C:\Program Files\FreeCommander\FreeCommander.exe
Run("runas /profile /user:DomainName\Administrator "C:\Program Files\FreeCommander\FreeCommander.exe"")
RunCMD("runas /profile /user:DomainName\Administrator "C:\Program Files\FreeCommander\FreeCommander.exe"")
We're in RunCMD
Enter the password for DomainName\Administrator:

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)[/small]
 
What is the objection to run it with .run() method?
 
Only that it opens a second window...

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)[/small]
 
What do you mean second window. second command prompt window? Is it then bad for any kind of second window?
 
Yes, it's nasty, but I might go with it anyway (or stick with my .bat file instead - there's not much advantage to using VBS in this case).

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)[/small]
 
>it's nasty
Why is that? you don't want to enter the password, if I understand what you meant by "second" window!
 
Besides, with .exec() method, you are guaranteed to be served a "displayed" window that you can't get away with without heavy hack. With .run() you may get rid of it easily. But you want to enter the password, do you not? (I want to see every morning some well-made coffee ready on my desk too, it is nasty without.)
 
No, no, the entering of the password is both necessary and desirable. I'm not trying to avoid that, I just prefer the aesthetics of having just one cscript/CMD window running instead of two, even if only briefly.

(Five minutes later):

Having edited the code to use the .run method then packaged it up with iExpress the .vbs version of the script now works as a .exe (previously the .exe result from iExpress would just close without the password prompt being seen).

iExpress gives an option to run the 'install' minimised so I'll be doing that.

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)[/small]
 
I never heard of iExpress. That looks pretty interesting. I already have a number of uses for that. Thanks.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
I'd not heard of it before yesterday! Seems a nice way of hiding your .VBS (or other) code from prying users.

The instructions on the first webpage I found didn't work but the second did. This one was good:
Essentially the important bit of correctness was specifying TWO script files in the package: The main one, plus a one-liner that calls the main script.

:)

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top