mogenheid

requireadmin is messing up an exe on a mapped drive

16 posts in this topic

Hello all, I have been searching for a few hours now. originally I was trying to figure out why i couldn't send keys to a program(program_A), but I found that #requireadmin fixed the issue. Now in the same script, I was running a different program(program_B) that was mapped on a network drive(G:), and it was working before i put in the #requireadmin. Now it will launch the program, but the program cannot find the files to monitor, which are on completely seperate mapped network drive(P:).

I have manually tried opening the exe, just double clicking it loads fine, but running admin, it gives the same errors

The errors are "Could not open P:\blah\something.log)

So I need the #requireadmin to allow me to send keys to program_A but using #requireadmin screws up program_B.

I am still new to autoit, any adivce or other threads I may not have seen yet would be appreciated.

Share this post


Link to post
Share on other sites



So i guess my question is, is there a way to get around this?

I am not the admin, I tried just running the program_A in a loop that went If isAdmin() then

but im not an admin, so that didnt work. derp

Maybe a do first, have the #requireadmin in that section, then the rest in the second?

I'll try looking up how to do that

Share this post


Link to post
Share on other sites

Just tested this on my PC which is in a workgroup and has a persistent P: drive mapped. Tried this simple script both with and without the first line and they return 1 for the fileexists():

#RequireAdmin
MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, 'Selection:' & @CRLF & 'FileExists("P:\jos\testfile.txt")' & @CRLF & @CRLF & 'Return:' & @CRLF & FileExists("P:\jos\testfile.txt")) ;### Debug MSGBOX

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Tried it with my file, returned 0, not found with #requireadmin, and I got a returned 1 without the #required admin.  

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

How is your drive mapped?  
Using a AD/Domain account?
Did you try using the UNC to the file ?  ( \\servername\share\path-to-file)

Jos

 

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Yup, AD/Domain, i'll try UNC

 

 

Share this post


Link to post
Share on other sites

Man, I am learning a lot of stuff from you. Found how to get the UNC, got the UNC, put it in the code, it opened the program, still cannot open the files on p drive.

I tried to bypass #requireadmin by putting it into a do until loop, but apparently once you call it in the script, it stays. the program still couldn't open the P drive file

 

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

The #RequireAdmin is an directive for autoit3 to run elevated so its "All or Nothing".
When the UNC doesn't work it would indicate that your elevated account doesn't have the rights to access the file. 
Maybe someone can test this as I doesn't have a AD/Domain setup at home to test with.

What happens when you open CMD with "Run as Administrator" from Windows Program menu and do a "Net use"command. Is the mapping still there and can you access the file?

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I guess I could put them in two separate script files, and have the the one without the #requireadmin do what it needs, then call the other script that has the require admin. I'll see if that works, but I am still curious about this issue.

Share this post


Link to post
Share on other sites
Quote
9 minutes ago, Jos said:

The #RequireAdmin is an directive for autoit3 to run elevated so its "All or Nothing".
When the UNC doesn't work it would indicate that your elevated account doesn't have the rights to access the file. 
Maybe someone can test this as I doesn't have a AD/Domain setup at home to test with.

What happens when you open CMD with "Run as Administrator" from Windows Program menu and do a "Net use"command. Is the mapping still there and can you access the file?

Jos

the P: drive si there but the status is unavailable

 

Share this post


Link to post
Share on other sites

I am using these UDFs in AutoIt3Wrapper to run a AutoIt3 Function or shell commands that require Admin level.

Func RunReqAdmin($Autoit3Commands, $prompt = 0)
    Local $temp_Script = _TempFile($TempDir, "~", ".au3")
    Local $temp_check = _TempFile($TempDir, "~", ".chk")
    FileWriteLine($temp_check, 'TempFile')
    FileWriteLine($temp_Script, '#NoTrayIcon')
    If Not IsAdmin() Then
        FileWriteLine($temp_Script, '#RequireAdmin')
        If $prompt = 1 Then MsgBox(262144, "Need Admin mode", "Admin mode is needed for this update. Answer the following prompts to allow the update.")
    EndIf
    FileWriteLine($temp_Script, $Autoit3Commands)
    FileWriteLine($temp_Script, "FileDelete('" & $temp_check & "')")
    RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $temp_Script & '"')
    ; Wait for the script to finish
    While FileExists($temp_check)
        Sleep(50)
    WEnd
    FileDelete($temp_Script & "*.*")
EndFunc   ;==>RunReqAdmin

Func RunReqAdminDosCommand($Autoit3Commands, $prompt = 0, $outfile = "")
    Local $temp_Script = _TempFile($TempDir, "~", ".au3")
    FileDelete($outfile)
    FileWriteLine($outfile, '! Cancelled execution.')
    FileWriteLine($temp_Script, '#NoTrayIcon')
    If Not IsAdmin() Then
        FileWriteLine($temp_Script, '#RequireAdmin')
        If $prompt = 1 Then MsgBox(262144, "Need Admin mode", "Admin mode is needed for this update. Answer the following prompts to allow the update.")
    EndIf

    __ConsoleWrite(">Running AdminLevel:" & $Autoit3Commands & @CRLF)
    FileWriteLine($temp_Script, "AutoItWinSetTitle ('AutoIt3Wrapper_RunningCommand')" )
    FileWriteLine($temp_Script, "$Pid = Run('" & @ComSpec & ' /C ' & $Autoit3Commands & "', ''," & @SW_HIDE & ", 2)")
    FileWriteLine($temp_Script, "$Handle = _ProcessExitCode($Pid)")
    FileWriteLine($temp_Script, "$ConOut = ShowStdOutErr($Pid)")
    FileWriteLine($temp_Script, "$ExitCode = _ProcessExitCode($Pid, $Handle)")
    FileWriteLine($temp_Script, "_ProcessCloseHandle($Handle)")
    FileWriteLine($temp_Script, 'FileDelete("' & $outfile & '")')
    If $outfile <> "" Then
        FileWriteLine($temp_Script, 'FileWrite("' & $outfile & '",$ConOut)')
    EndIf
    FileWriteLine($temp_Script, "Exit $ExitCode")
    FileWriteLine($temp_Script, "Func _ProcessCloseHandle($h_Process)")
    FileWriteLine($temp_Script, "   ; Close the process handle of a PID")
    FileWriteLine($temp_Script, "   DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $h_Process)")
    FileWriteLine($temp_Script, "   If Not @error Then Return 1")
    FileWriteLine($temp_Script, "   Return 0")
    FileWriteLine($temp_Script, "EndFunc   ;==>_ProcessCloseHandle")
    FileWriteLine($temp_Script, "Func _ProcessExitCode($i_Pid, $h_Process = 0)")
    FileWriteLine($temp_Script, "   ; 0 = Return Process Handle of PID else use Handle to Return Exitcode of a PID")
    FileWriteLine($temp_Script, "   Local $v_Placeholder")
    FileWriteLine($temp_Script, "   If Not IsArray($h_Process) Then")
    FileWriteLine($temp_Script, "       ; Return the process handle of a PID")
    FileWriteLine($temp_Script, "       $h_Process = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'int', 0x400, 'int', 0, 'int', $i_Pid)")
    FileWriteLine($temp_Script, "       If Not @error Then Return $h_Process")
    FileWriteLine($temp_Script, "   Else")
    FileWriteLine($temp_Script, "       ; Return Process Exitcode of PID")
    FileWriteLine($temp_Script, "       $h_Process = DllCall('kernel32.dll', 'ptr', 'GetExitCodeProcess', 'ptr', $h_Process[0], 'int*', $v_Placeholder)")
    FileWriteLine($temp_Script, "       If Not @error Then Return $h_Process[2]")
    FileWriteLine($temp_Script, "   EndIf")
    FileWriteLine($temp_Script, "   Return 0")
    FileWriteLine($temp_Script, "EndFunc   ;==>_ProcessExitCode")
    FileWriteLine($temp_Script, "Func ShowStdOutErr($l_Handle, $ShowConsole = 1, $Replace = '', $ReplaceWith = '')")
    FileWriteLine($temp_Script, "   Local $Line = 'x', $Line2 = 'x', $tot_out, $err1 = 0, $err2 = 0, $cnt1 = 0, $cnt2 = 0")
    FileWriteLine($temp_Script, "   Do")
    FileWriteLine($temp_Script, "       Sleep(10)")
    FileWriteLine($temp_Script, "       $Line = StdoutRead($l_Handle)")
    FileWriteLine($temp_Script, "       $err1 = @error")
    FileWriteLine($temp_Script, "       If $Replace <> '' Then $Line = StringReplace($Line, $Replace, $ReplaceWith)")
    FileWriteLine($temp_Script, "       $tot_out &= $Line")
    FileWriteLine($temp_Script, "       If $ShowConsole Then ConsoleWrite($Line)")
    FileWriteLine($temp_Script, "       $Line2 = StderrRead($l_Handle)")
    FileWriteLine($temp_Script, "       $err2 = @error")
    FileWriteLine($temp_Script, "       If $Replace <> '' Then $Line2 = StringReplace($Line2, $Replace, $ReplaceWith)")
    FileWriteLine($temp_Script, "       $tot_out &= $Line2")
    FileWriteLine($temp_Script, "       If $ShowConsole Then ConsoleWrite($Line2)")
    FileWriteLine($temp_Script, "       ; end the loop also when AutoIt3 has ended but a sub process was shelled with Run() that is still active")
    FileWriteLine($temp_Script, "       ; only do this every 50 cycles to avoid cpu hunger")
    FileWriteLine($temp_Script, "       If $cnt1 = 50 Then")
    FileWriteLine($temp_Script, "           $cnt1 = 0")
    FileWriteLine($temp_Script, "           ; loop another 50 times just to ensure the buffers emptied.")
    FileWriteLine($temp_Script, "           If Not ProcessExists($l_Handle) Then")
    FileWriteLine($temp_Script, "               If $cnt2 > 2 Then ExitLoop")
    FileWriteLine($temp_Script, "               $cnt2 += 1")
    FileWriteLine($temp_Script, "           EndIf")
    FileWriteLine($temp_Script, "       EndIf")
    FileWriteLine($temp_Script, "       $cnt1 += 1")
    FileWriteLine($temp_Script, "   Until ($err1 And $err2)")
    FileWriteLine($temp_Script, "   Return $tot_out")
    FileWriteLine($temp_Script, "EndFunc   ;==>ShowStdOutErr")
    $rc = RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $temp_Script & '"')
    sleep(500)
    ; Wait for the script to finish
    While WinExists("AutoIt3Wrapper_RunningCommand")
        Sleep(50)
    WEnd
    __DelTempFile($temp_Script)
    $rc = FileRead($TempConsOut)
    __DelTempFile($TempConsOut)
    Return $rc
EndFunc   ;==>RunReqAdminDosCommand

The $OutFile parameter of the second UDF will contain any Console output from the DOS command.
The first UDF currently doesn't any feedback option but that can be added.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I'm really tired man. My eyes are starting to hurt.  I am not sure what your program does, I'll try to interpret it tomorrow. I tried to call the #requireadmin after my other two program calls, but just including other script with #requireadmin brings it in to the main script before its run. Like you said  "All or Nothing".

 

So I was able to make a work around by calling the two scripts from a vbs file instead. This way I was able to work round the #require admin.

Thanks for all of your input man

Share this post


Link to post
Share on other sites
Quote
Just now, mogenheid said:

I'm really tired man. My eyes are starting to hurt.  I am not sure what your program does, I'll try to interpret it tomorrow. I tried to call the #requireadmin after my other two program calls, but just including other script with #requireadmin brings it in to the main script before its run. Like you said  "All or Nothing".

 

So I was able to make a work around by calling the two scripts from a vbs file instead. This way I was able to work round the #require admin.

Thanks for all of your input man

Sorry, i meant: I tried to call the second script that had the #requireadmin after i ran my first two program calls. That didnt work 

 

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

You can also get around this limitation by using script re-execution with ShellExecute with the "runas" verb. This is a more controlled way to do a #RequireAdmin.  Here is an example.  

;AutoIt commands that need to run as admin.
If IsAdmin() Then
    MsgBox(0, "IsAdmin Test", "This part of the script is running under admin." & @CRLF & @CRLF & "IsAdmin:  " & IsAdmin() & @CRLF & @CRLF & "Second execution of the script.")  
    
    Exit
EndIf

;Auto commands that are not admin. 
MsgBox(0, "IsAdmin Test", "This part of the script is NOT running under admin." & @CRLF & @CRLF & "IsAdmin:  " & IsAdmin() & @CRLF & @CRLF & "First execution of the script.")  

;Re-run the script as admin and execute admin part.
_RunAdminPartofScript()

Func _RunAdminPartofScript()
    Local $sParameters = ""
    If Not @Compiled Then
        $sParameters = ' "' & @ScriptFullPath & '"'
    EndIf

    If ShellExecute(@AutoItExe, $sParameters, "", "runas") Then
        Exit
    Else
        Exit MsgBox(16 + 262144, "ERROR!", "Unable to run as admin.")
    EndIf
    
EndFunc

 

Adam

Edited by AdamUL

Share this post


Link to post
Share on other sites
15 hours ago, AdamUL said:

You can also get around this limitation by using script re-execution with ShellExecute with the "runas" verb. This is a more controlled way to do a #RequireAdmin.  Here is an example.  

;AutoIt commands that need to run as admin.
If IsAdmin() Then
    MsgBox(0, "IsAdmin Test", "This part of the script is running under admin." & @CRLF & @CRLF & "IsAdmin:  " & IsAdmin() & @CRLF & @CRLF & "Second execution of the script.")  
    
    Exit
EndIf

;Auto commands that are not admin. 
MsgBox(0, "IsAdmin Test", "This part of the script is NOT running under admin." & @CRLF & @CRLF & "IsAdmin:  " & IsAdmin() & @CRLF & @CRLF & "First execution of the script.")  

;Re-run the script as admin and execute admin part.
_RunAdminPartofScript()

Func _RunAdminPartofScript()
    Local $sParameters = ""
    If Not @Compiled Then
        $sParameters = ' "' & @ScriptFullPath & '"'
    EndIf

    If ShellExecute(@AutoItExe, $sParameters, "", "runas") Then
        Exit
    Else
        Exit MsgBox(16 + 262144, "ERROR!", "Unable to run as admin.")
    EndIf
    
EndFunc

 

Adam

What is the difference between IsAdmin() and #RequireAdmin? Since I am not admin, nothing in a If IsAdmin() then loop will run for me, because I am not an admin on this computer. #RequireAdmin does work for me though.

As I see it in the documentation, #requireadmin: " This function was primarily aimed at allowing AutoIt scripts to work correctly with Windows User Account Control (UAC) "

and IsAdmin() checks to see if you have admin of the computer. 

Am I missing something? Will the "runas" verb allow me to execute that function with admin rights even though I don't have them, while leaving the privilege of having admin rights out of the rest of the code?

Share this post


Link to post
Share on other sites

I'm confused by what you want then.  I thought you wanted to run some commands without the Admin Token, and other with.  The IsAdmin function checks to see if the current user the script is running under has the Admin Token.  Being in the local Administrators group, is not the same as having the Admin Token.  They are two different things.  You do have to be in the local Administrators group to be able to request the Admin Token, and not be denied.  The #RequireAdmin directive causes the whole script to be re-executed, requesting the Admin Token.  The "runas" verb for the ShellExecute function requests the Admin Token for the process executed by the function with the current user.  

No, the "runas" verb will not allow you to run something with the Admin Token, unless the account you are running under is in the local Administrators group.  You will see a UAC prompt requesting an admin account.  

 

Adam

Share this post


Link to post
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