Sign in to follow this  
Followers 0
nikink

RunWait over network not working

2 posts in this topic

Hi all,

I have a script that used to work when run from a mapped network drive, now it doesn't. Only thing I can think of that's changed is that I've recently updated to the latest AutoIt and Beta versions and Scite.

Has anyone else had this problem?

Here's the testing script taken from my larger one and stripped to essentials for debug purposes...

#include <string.au3>
#region Main Program

For $i = 1 To 2 ; Ensures the script will run all the functions twice.
    MsgBox(0,$i,"Running", 2)
Next

#endregion
Exit ;- Don't need this if OnAutoItStart is being used
; End of Script.

; AutoRun "Script.exe" as Admin ; By MHz on AutoIt Forums

Func OnAutoItStart()

    Local $User = _StringEncrypt(0, "ENCRYPTEDSTRING1", "PASSWORD", "1")

    Local $Domain = _StringEncrypt(0, "ENCRYPTEDSTRING2", "PASSWORD", "1")

    Local $Pass = _StringEncrypt(0, "ENCRYPTEDSTRING3", "PASSWORD", "1")
    ;
    ; Set a working directory for the script.
    FileChangeDir(@TempDir)

    ; Check incoming parameters
    If Not $CMDLINE[0] Then
    
        RunAsSet($User, $Domain, $Pass)
        RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /mapprintershare', @TempDir)
        RunAsSet()

        Exit
    ElseIf $CMDLINE[1] <> '/mapprintershare' Then
        ; Exit script with exit code 1
        Exit 1
    EndIf
EndFunc   ;==>OnAutoItStart

Share this post


Link to post
Share on other sites



If you are using Windows XP, there is a bug I have found with the RunAs features of XP. I think AutoIT uses the same set of Windows code that is used in MS's RunAs command. The problem is running an item under alternate credentials from the logged in user. If you try to run an app with alternate credentials on your local computer, then it works without flaw. If you try to run the app from a network with alternate credentials, it will fail hands down. The reason behind this is a bug that doesn't like virtual paths or drive letters. The script will only run if you convert everything to a UNC path as far as the network is concerned. You will also need to make the working directory equal to the directory where your executable is being run from. I don't know why this is, all I know this is the only way to make it work.

The following code should solve your issue.

Func OnAutoItStart()

    Local $User = _StringEncrypt(0, "ENCRYPTEDSTRING1", "PASSWORD", "1")

    Local $Domain = _StringEncrypt(0, "ENCRYPTEDSTRING2", "PASSWORD", "1")

    Local $Pass = _StringEncrypt(0, "ENCRYPTEDSTRING3", "PASSWORD", "1")

    ; Check incoming parameters
    If Not $CMDLINE[0] Then

        $appToRun = '"' & @AutoItExe & '"'
        $args = '/AutoIt3ExecuteScript "' & _convertToUNC(@ScriptFullPath) & '" /mapprintershare'

        $workingDir = ""

        $pathArr = StringSplit($appToRun, "\")
        $arrSize = $pathArr[0]
        _ArrayDelete($pathArr, $arrSize)
        _ArrayDelete($pathArr, 0)

        For $i = 1 to UBound($pathArr)
            $workingDir = $workingDir & $pathArr[$i-1] & "\"
        Next

        $appToRun = _convertToUNC($appToRun)
        $workingDir = _convertToUNC($workingDir) & "\"

        RunAsSet($User, $Domain, $Pass)
        RunWait($appToRun & " " & $args, $workingDir)
        RunAsSet()
        Exit
    ElseIf $CMDLINE[1] <> '/mapprintershare' Then
            ; Exit script with exit code 1
            Exit 1
    EndIf
EndFunc   ;==>OnAutoItStart
 
Func _convertToUNC($path)
    Local $pArr, $drvLetter, $drvs, $UNC, $nPath = ""
    Local $networkDrv = False
    
    If StringLeft($path, 2) == "\\" Then
        Return $path
    EndIf   
    
    $pArr = StringSplit($path, "\")
    
    $drvLetter = $pArr[1]
    $drvs = DriveGetDrive("Network")
    
    For $i = 1 to $drvs[0]
        If StringUpper($drvLetter) == StringUpper($drvs[$i]) Then
            $networkDrv = True
            ExitLoop
        EndIf
    Next
    
    If $networkDrv Then
        _ArrayDelete($pArr, 1)
        $pArr[0] = $pArr[0]-1
        
        $UNC = DriveMapGet($drvLetter)
        
        $nPath = $UNC & "\"
        
        For $i = 1 to $pArr[0]
            If $i <> $pArr[0] Then
                $nPath = $nPath & $pArr[$i] & "\"
            Else
                $nPath = $nPath & $pArr[$i]
            EndIf
        Next
        
        If StringRight($nPath, 1) == "\" Then $nPath = StringLeft($nPath, StringLen($nPath)-1)
        
        Return $nPath
    Else
        If StringRight($path, 1) == "\" Then $path = StringLeft($path, StringLen($path)-1)
        
        Return $path
    EndIf
EndFunc

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
Sign in to follow this  
Followers 0