triodz Posted January 5, 2012 Share Posted January 5, 2012 (edited) 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 January 5, 2012 by triodz Link to comment Share on other sites More sharing options...
Chimaera Posted January 5, 2012 Share Posted January 5, 2012 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 If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
triodz Posted January 5, 2012 Author Share Posted January 5, 2012 Thanks for the input Chimaera, but as mentioned, this is on XP, so #RequireAdmin does not help, unfortunately. I tried, as clutching at straws is what I do best, but was unsuccessful. Link to comment Share on other sites More sharing options...
kylomas Posted January 6, 2012 Share Posted January 6, 2012 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...expandcollapse popup#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 More sharing options...
kylomas Posted January 6, 2012 Share Posted January 6, 2012 (edited) triodz, Oops, forgot to mention that I do NOT have an XP platform currently so cannot test the auth issues. Good Luck, kylomas Edited January 6, 2012 by 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 More sharing options...
triodz Posted January 9, 2012 Author Share Posted January 9, 2012 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 More sharing options...
kylomas Posted January 9, 2012 Share Posted January 9, 2012 triodz, $rc is a local variable that I used to test the results of function calls. Get in the habit of doing this and save lot's of frustration later on... 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 More sharing options...
water Posted January 9, 2012 Share Posted January 9, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now