Jump to content

Adding text to a file, but need to open/save as an administrator


Recommended Posts

I need to append to an .ini file with two lines of text, however need to open/save the file using elevated credentials.

Was using the following syntax which I've read that you cannot use RunAs with FileOpen/FileWrite, are there any alternatives? 

$file = RunAs("administrator", @ComputerName, $AdminPW, 4, FileOpen("C:Program FilesApplicationClientBinconfig.ini", 1))

     If $file = -1 Then
     MsgBox(0, "Error", "Unable to open file.")

     Else

RunAs("administrator", @ComputerName, $AdminPW, 4, FileWrite($file, "[Plugins]"))

RunAs("administrator", @ComputerName, $AdminPW, 4, FileWrite($file, "" & @CRLF)) ; The CRLF at the end is a line break
RunAs("administrator", @ComputerName, $AdminPW, 4, FileWrite($file, "AcrobatPlugin.NichePlugin.dll=1"))

RunAs("administrator", @ComputerName, $AdminPW, 4, FileWrite($file, "" & @CRLF)) ; The CRLF at the end is a line break

Link to comment
Share on other sites

A solution can be to RunAs the script itself with an admin account, so everything will be run with admin rights

For example :

_RunAsAdmin("Administrator", "P@$$w0rd!")
If @error Then Exit MsgBox(16, "Error", "Unable to run the script with the specified admin account.")

MsgBox(0, "", "Now, this program is running with """ & @UserName & """ account")
; The rest of the script will run in the admin account



Func _RunAsAdmin($sUsername, $sPassword)
    If IsAdmin() Then Return 1

    RunAs($sUsername, @ComputerName, $sPassword, 0, @AutoItExe & " " & $CmdLineRaw, @WorkingDir)
    If @error Then Return SetError(1, 0, 0)

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