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:
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:
If I change /k as the Command Interpreter argument to /c the script just ends:
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]
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
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.
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]