Jump to content

Need code to run as an administrator


 Share

Recommended Posts

I need a portion of my code to run as an administrator. Basically I need to insert the username and password of an administration account for the code to do what I want without interaction from our helpdesk. It will save us and some of our users a bit of time.

Code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1_1 = GUICreate("Form1", 187, 109, 192, 114)
$Label1 = GUICtrlCreateLabel("Enter your Username", 8, 8, 170, 22)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")

$input = GUICtrlCreateInput("", 16, 32, 145, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
$data = GUICtrlRead($input)

$MyButton1 = GUICtrlCreateButton("OK", 56, 64, 68, 30, BitOR($BS_NOTIFY,$BS_FLAT))
GUICtrlSetOnEvent($MyButton1, "Username")
GUISetState(@SW_SHOW)

Opt("GUIOnEventMode", 1)

While 1
   sleep(10)
WEnd

func Username ()
   ;need below to run as administrator
   ProcessClose("iexplore")
   DirRemove("c:documents and settings" & $input & "cookies")
   DirRemove("c:documents and settings" & $input & "local settingstemporary internet files")
   Exit
EndFunc

Exit

Once it is in an executable, no one will be able to find the admin pass of course (unless they want to go through reverse engineering it of course. Not in our organization).

Running on Win XP SP3

Any help is appreciated.

Edited by triodz
Link to comment
Share on other sites

Can the whole script be run as admin?

Add

#RequireAdmin

at the top of the script, who ever runs it will still get the UAC promt when you run it but it will run as admin after

There is some scripts on here to elevate and deelevate admin if you search which may help

Try here

Link to comment
Share on other sites

triodz,

There are some general scripting issues that you need to address before you work out the auth level issues:

1 - Options are customarily set near the top of the script (as are all script-wide definitions). While

"Opt("GUIOnEventMode", 1)" works anywhere before the while loop it is poor coding practice.

2 - You are in "event" mode but do not provide a way to exit the script gracefully. Using an "exit" function

provides this as well as use of the dialog close control (not sure if that is the right terminology).

3 - $input is the controlid for the input control. Use "guictrlread" to get it's value. Moreover,

this control is not actioned so the value of the control is not available until function "username" is entered

after actioning $MyButton1.

4 - Results from the DIR commands are not checked...

The following is a re-work of your code...

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

$Form1_1   = GUICreate("Form1", 187, 109, 192, 114)
             GUISetOnEvent($gui_event_close,'_fini')
$Label1    = GUICtrlCreateLabel("Enter your Username", 8, 8, 170, 22)
             GUICtrlSetFont(-1, 12, 400, -1, "Arial")
$input     = GUICtrlCreateInput("", 16, 32, 145, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
$MyButton1 = GUICtrlCreateButton("OK", 56, 64, 68, 30, BitOR($BS_NOTIFY,$BS_FLAT))
             GUICtrlSetOnEvent($MyButton1, "Username")
             GUISetState(@SW_SHOW)

While 1
   sleep(10)
WEnd

func Username ()
    Local $rc
    ;need below to run as administrator
    ProcessClose("iexplore")
    $rc = DirRemove("c:documents and settings" & GUICtrlRead($input) & "cookies")
    If $rc = 0 Then MsgBox(0,'DIR Remove Error','Error Removing ' & GUICtrlRead($input) & ' cookies directory')
    $rc = DirRemove("c:documents and settings" & GUICtrlRead($input) & "local settingstemporary internet files")
    If $rc = 0 Then MsgBox(0,'DIR Remove Error','Error Removing ' & GUICtrlRead($input) & ' temp ie directory')
EndFunc

Func _fini()

    MsgBox(0,"Finished!!!","Exiting..." & @LF & @LF & "Pass results info or whatever here....",3)
    exit

endfunc

Good Luck,

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thanks for your help kylomas. I headed home for the weekend before I got a chance to see your post (I can't test what I need to do at home). I ended up figuring it out, as per below:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1_1 = GUICreate("Form1", 188, 131, 192, 114)
$Label1 = GUICtrlCreateLabel("Enter your ADM number", 8, 8, 170, 22)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
$input = GUICtrlCreateInput("", 16, 32, 145, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
$data = GUICtrlRead($input)
$MyButton1 = GUICtrlCreateButton("OK", 56, 64, 68, 30, BitOR($BS_NOTIFY,$BS_FLAT))
GUICtrlSetOnEvent($MyButton1, "adm")
$Please = GUICtrlCreateLabel("Please wait for ""Complete"" message", 8, 104, 175, 17)
GUISetState(@SW_SHOW)
Opt("GUIOnEventMode", 1)
While 1
sleep(10)
WEnd
func adm ()
$data = GUICtrlRead($input)
RunAsWait("Username","Domain","Password",2,@ComSpec & " /c pskill iexplore","",@SW_HIDE)
RunAsWait("Username","Domain","Password",2,@ComSpec & " /c rmdir /s /q ""C:Documents and Settings"&$data&"cookies")
RunAsWait("Username","Domain","Password",2,@ComSpec & " /c rmdir /s /q ""C:Documents and Settings"&$data&"Local settingsTemporary internet files")
if FileExists("C:Documents and Settings"&$data&"cookies*.txt") Then
MsgBox (0,"","Failed  :(")
    Exit
Else
MsgBox (0,"","Complete! :)")
    Exit
endif
EndFunc
Exit

Once it is in an executable, the username and password will be inaccessible. I could not see where I would put the Admin credentials in your script? I am assuming "Local $rc" is what you were using. Our machine unfotunately do not have local administrator accounts, but do have an admin group built into it. This is where the difficulty started.

Now I am trying add a progress bar that shows percentage done. Not proving easy!

Thanks again guys.

Link to comment
Share on other sites

Once it is in an executable, the username and password will be inaccessible.

Not true!

Nothing you put in a compiled AutoIt script is 100% save! Please search the forums - you'll find many threads discussing this subject.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...