Jump to content

Help with Deleting a file using Admin Credentials


Recommended Posts

I've searched and haven't found exactly what I'm looking for and I'm hoping someone can look at my syntax and tell me where I'm noobing it all up.

I'm simply trying to delete a single file in All Users startup folder of Windows 7 x64 (which is c:\programdata\microsoft\windows\start menu\programs\startup).

My issue is that I need the AutoIT executable to run for non-admin accounts, so I can just use an AutoIT call to delete a file, as it will require admin rights. Users can't simply "right-click and run-as-admin", I need to provide the credentials for them.

I thought I could simply issue the following command but it is failing me:

RunAs("{myadminaccountname", '{mydomainname', "{myadminaccountpassword}", 0, @ComSpec & " /c del" & @ProgramsCommonDir & '\startup\congratulations.exe')

So my goal is to delete the file "c:\programdata\microsoft\windows\start menu\programs\startup\congratulations.exe". If anyone could help me with my syntax or suggest a better method to do this I would greatly appreciate it.

Link to comment
Share on other sites

Yap I got it

This May Help U out

#RequireAdmin
If Not IsAdmin() Then
MsgBox(16,"Information","Sorry Autoit Wasn't Able To Get Admin Rights"&@CRLF&"Closing In 5 Seconds......",6)
Exit -1
Else
MsgBox(64,"Information","Successfully Started With Admin Rights"&@CRLF&"Closing In 5 Seconds......",6)
Exit 1
EndIf

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Orelse Here is the Full CODE

Cheers Up If it helped u out

#RequireAdmin
If Not IsAdmin() Then
MsgBox(16,"Information","Sorry Autoit Wasn't Able To Get Admin Rights"&@CRLF&"Closing In 5 Seconds......",6)
Exit -1
Else
MsgBox(64,"Information","Successfully Started With Admin Rights"&@CRLF&"Closing In 5 Seconds......",6)
_Main()
Exit 1
EndIf
Func _Main()
Local $dir  =  @StartupCommonDir&''
Local $search =  FileFindFirstFile($dir&'*.*')
While True
  Sleep(10)
  Local $file =  FileFindNextFile($search)
  If @error Then   ExitLoop
  If @extended Then  ;Returns @extended if it is a Directory
   DirRemove($file)
  Else   ;File Deletion
   FileDelete($file)
  EndIf
WEnd
If @error Then Return -1
Return 0
EndFunc

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

PhoenixXL, I appreciate the response but it really doesn't help me achieve what I was looking for. This triggers UAC and doesn't do anything about running the DEL command with elevated privileges. I am needing an AutoIT executable that just silently deletes a very specific file for standard user who aren't privileged (admins) and will not be given an admin account or password.

For example I have a successful AutoIT executable that using the following script to register a DLL and .Net assembly for non-admin users:

runas("{adminusername}", "{mydomainname}", "{adminuserpassword}", 0, 'C:WindowsMicrosoft.NETFrameworkv4.0.30319regasm "C:Program Files (x86)KI SystemsKI14.dll" /codebase /silent')

runas("{adminusername}", "{mydomainname}", "{adminuserpassword}", 0, 'C:windowssyswow64regsvr32 "C:Program Files (x86)KI SystemsKI14Shim.dll" /s')

This script (made into an executable) just runs silently and does exactly what I needed to register these two files for non-admins. I thought I could simply do the same thing with DEL command to delete the file I need, but so far every way I've thought of to structure the command it fails. I hope that makes sense.

Again, thanks for the help!

Link to comment
Share on other sites

How about something like this?

RunAs("username", "domain", "password", 0, @ComSpec & " /k Del /?", "", show_flag)

Edit:Change the /k to /c to automatically close the window when it's done. Of course, you'd also have to change the /? to the file you want to delete, and change show_flag to @SW_HIDE if you don't want the console window to show when it's run.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Brewman, thanks and I have follow up:

RunAs("username", "domain", "password", 0, @ComSpec & " /k Del /C:ProgramDataMicrosoftWindowsStart MenuProgramsStartupCongratulations.EXE", "", show_flag) is giving me error parsing function call which I'm assuming is a syntax error.

I've tried several permutations of this with double quotes around the path/filename, single quotes around path/filename, no quotes around path/filename. I've tried both the shortened StartM~1 name to avoid space in the path, and with Start Menu with space left in. As you can see in my original post I even found the variable @ProgramsCommonDir that represents C:ProgramDataMicrosoftWindowsStart MenuPrograms (which I verified by issuing the same basic call with explorer.exe instead to see if the correct folder opened up) that I thought would make the command more straightforward. In your sample you show /? and said replace the ? with the path/filename and so I also tried with the / left in front of the path/filename and without.

All ways are failing, so how would you structure your example for the exact path/filename that I have?

Link to comment
Share on other sites

As stated in my edit above, you need to change show_flag to something other than "show_flag", it's only there as a place holder from the help file example.

There is a built-in function called FileGetShortName that returns the Windows 8.3 file name so you don't have to worry about where your quotes go around the file path that might make things easier.

$FileName = FileGetShortname("C:ProgramDataMicrosoftWindowsStart MenuProgramsStartupCongratulations.EXE") ; this gets the 8.3 file name of the file you want to delete.
RunAs("username", "domain", "password", 0, @ComSpec & " /c Del " & $FileName, "", @SW_HIDE) ; this will run the comspec program (cmd.exe) hidden, then close the window when it's done.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BrewManNH, thanks for all your help. I wasn't paying attention to the "show_flag" piece, bonehead move on my part. Still not working so I must be running up against some file permission/system file type error that I'm missing. I'm going to play around with a command prompt and attempt the same move using RunAs from dos and see what the behavior is.

Link to comment
Share on other sites

If you remove the /c from after the @Comspec and also the @SW_HIDE it will display the console window and might provide you with some insight as to what is happening to cause it to fail.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Yeah, when I just as a test run from a command prompt "runas /noprofile /user:{domain}{adminaccount} del" I get prompted for password (as expected using a DOS runas where you can't pass through the password) and after supplying the password I get "RUNAS ERROR: Unable to run - del 2: The system cannot find the file specified". I have to call Del some other way.
Link to comment
Share on other sites

When I run this using my admin credentials on a Win7 machine, and a file that I copied to the location for testing, it deletes it without a problem. If I mark it as read only, I get an access denied message, but if it's not locked it gets deleted.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BrewManNH, I do appreciate all your time looking at my weird issue. I have no idea what's going on. I started dinking around and copied a txt file to the location and tried deleting it also with no luck. I'm thinking I'm running up against some domain security issue. For now I'm just moving on and rethinking my approach. I just got hit with Microsoft no allowing non-admins from doing a RunOnce entry in HKLM, non-admins can only do RunOnce that is in HKCU and needed a way to run once for the next user to log on.

Link to comment
Share on other sites

Do you have the local administrator's credentials? Because you can use those instead of a domain admin's. Substitute @ComputerName in place of "domain" and see if that works.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BrewManNH, great call! Never even thought of using my local admin. That worked perfectly. Makes no sense, but doing that worked.

For anyone else stumbling on this, here is the script that worked perfectly.

$FileName = FileGetShortname("C:ProgramDataMicrosoftWindowsStart MenuProgramsStartupCongratulations.EXE")

RunAs("administrator", @ComputerName, "{adminspassword}", 0, @ComSpec & " /c Del " & $FileName, "", @SW_HIDE)

Thank you so much BrewManNH for sticking with me on this till we worked it out! Also thank you PhoenixXL for your input, its nice to know people are paying attention to forum posts.

Link to comment
Share on other sites

For anyone's benefit stumbling on this post later, I also added some code before the delete code to ensure the process of the exe was not still running, which of course would prevent deletion.

_ProcessCloseEx("Congratulations.EXE")

Func _ProcessCloseEx($sPID)

If IsString($sPID) Then $sPID = ProcessExists($sPID)

If Not $sPID Then Return SetError(1, 0, 0)

Return Run(@ComSpec & " /c taskkill /F /PID " & $sPID & " /T", @SystemDir, @SW_HIDE)

EndFunc

Sleep (4000) ;sleep 4 seconds to give process time to end

Link to comment
Share on other sites

Hey wdjenkins!!!

At Last You Got It ............

Congratulationss.........

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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...