使用psexec在远程计算机上运行AutoIt

时间:2014-08-14 00:56:15

标签: python autoit psexec

我正在尝试在远程计算机上运行AutoIt脚本。

psexec.exe -accepteula \\remotemachine -u admin -p password "C:\Program Files\AutoIt3\AutoIt3.exe" "C:\Users\admin\runNotepad.au3"

我正在尝试从脚本中打开一个记事本并在其中写入内容。我也在写脚本中的一些日志。虽然我可以看到日志,但我无法看到屏幕上发生任何事情。

我尝试使用参数-i 0打开一个交互式屏幕,该屏幕与在本地计算机上运行脚本不同。还有其他办法吗?

3 个答案:

答案 0 :(得分:3)

最后我能够弄明白。我们应该首先找到该用户的远程机器的登录会话ID。我首先使用psexec运行qwinsta命令来检查会话ID

psexec \\remote -u admin -p password  qwinsta

这给了我所有会话的列表。在活动会话中签出具有与之关联的用户名的会话。 就我而言,它是2。

然后我运行此命令,会话ID为2

psexec.exe -i 2 -accepteula \\remotemachine -u admin -p password "C:\Program Files\AutoIt3\AutoIt3.exe" "C:\Users\admin\runNotepad.au3"

答案 1 :(得分:1)

这有用吗?

#include <Date.au3>
#include <File.au3>


_LaunchProgramOnRemoteComputer("192.168.50.0", "TEST-PC", "usertest", "passtest", "D:\programToExecute.exe", "", True, "15")


; #FUNCTION# ====================================================================================================================
; Name...........: _LaunchProgramOnRemoteComputer
; Description ...: Copy and execute a program on remote computer.
;
; Syntax.........: _LaunchProgramOnRemoteComputer($ipaddress, $domain, $username, $password, $program[, $parameters = ""[, $show = True[, $timeout = "15"]]])
;
; Parameters ....: $ipadress    - IP Address of the remote computer.
;                 $domain     - Active Directory domain name or Remote computer name.
;                 $username - Username of the user who execute the program on the remote computer.
;                 $password - Password of the user who execute the program on the remote computer.
;                 $program      - Local path of the program to execute.
;                 $parameters  - Parameters of the program.
;                 $show     - Display the interaction with the remote Desktop (True or False).
;                 $timeout   - Timeout in seconds (like "20").
;
; Return values .: Success    - Returns the return code of the program executed.
;                 Failure     - 0  and sets @ERROR
;                                            1 : Timeout.
;                                            2 : PsExec service failed to start on the remote computer.
;                                            3 : The program could not be executed.
;
; Author ........: Jeremy Guillot
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _LaunchProgramOnRemoteComputer($ipaddress, $domain, $username, $password, $program, $parameters = "", $show = True, $timeout = "15")

    ; We decompose the path of the program.
    Local $sProgramDrive, $sProgramDir, $sProgramFName, $sProgramExt
    _PathSplit($program, $sProgramDrive, $sProgramDir, $sProgramFName, $sProgramExt)

    ; Delete the file on the remote machine.
    RunAsWait($username, $domain, $password, 2, @ComSpec & ' /c del /F \\"' & $ipaddress & "\c$\" & $sProgramFName & $sProgramExt, "", @SW_HIDE)

    ; Copy the program on sharing 'c$' on the remote machine.
    RunAsWait($username, $domain, $password, 2, @ComSpec & ' /c copy /Y "' & $program & '" \\"' & $ipaddress & "\c$\" & $sProgramFName & $sProgramExt, "", @SW_HIDE)

    ; Parameters of the program.
    If $parameters <> "" Then $parameters = " " & $parameters

    ; Display the interaction with the remote Desktop.
    If $show Then
        $show = " -i"
    Else
        $show = ""
    EndIf

    ; Program execution.
    Local $iStdoutg = Run(@comspec & " /c PsExec \\" & $ipaddress & " -u " & $domain & "\" & $username & " -p " & $password & $show & " -h -n " & $timeout & " C:\" & $sProgramFName & $sProgramExt & $parameters, @ScriptDir & "\Tools\PsTools\", @SW_HIDE, 6)
    Local $sTimeoutBegin = _NowCalc()
    Local $sCommandResult = ""
    Local $sCurrentLine = ""
    While True
        If _DateDiff("s", $sTimeoutBegin, _NowCalc()) > $timeout Then
            ProcessClose("PsExec.exe")
            ProcessClose("PSEXESVC.exe")
            Return SetError(1, 0, 0)
        EndIf
        $sCurrentLine = StderrRead($iStdoutg)
        If @error Then ExitLoop
        If $sCurrentLine <> "" Then
            $sCommandResult = $sCommandResult & @CRLF & $sCurrentLine
        EndIf
    WEnd
    ;If $sCommandResult <> "" Then ConsoleWrite($sCommandResult & @CRLF)

    ; Closing the PsExec process in case they would not shut.
    ProcessClose("PsExec.exe")
    ProcessClose("PSEXESVC.exe")

    ; Remove the program on the remote machine.
    RunAsWait($username, $domain, $password, 2, @ComSpec & ' /c del /F \\"' & $ipaddress & "\c$\" & $sProgramFName & $sProgramExt, "", @SW_HIDE)

    ; Error handling.
    Local $bServiceStarted = False
    Local $bCommandExecuted = False
    Local $bCommandFinished = False
    If StringInStr($sCommandResult, "Connecting with PsExec service on") And Not $bServiceStarted Then $bServiceStarted = True
    If StringInStr($sCommandResult, "Starting " & $program & " on") And Not $bCommandExecuted Then $bCommandExecuted = True
    If StringInStr($sCommandResult, " exited on " & $ipaddress & " with error code") And Not $bCommandFinished Then $bCommandFinished = True

    If $bCommandFinished Then
        ; We get the return code of the program.
        $sCommandResult = StringStripCR(StringStripWS(StringStripWS($sCommandResult, 1), 2))
        Local $sCommandResultCodePosition = StringInStr($sCommandResult, "with error code ")
        $sCommandResult = StringTrimLeft($sCommandResult, $sCommandResultCodePosition)
        $sCommandResult = StringTrimLeft($sCommandResult, 15)
        $sCommandResult = StringTrimRight($sCommandResult, 1)
    Else
         If Not $bServiceStarted Then Return SetError(2, 0, 0)
         If Not $bCommandExecuted Then Return SetError(3, 0, 0)
    EndIf

     Return SetError(0, 0, $sCommandResult)

EndFunc

答案 2 :(得分:0)

您使用的操作系统是什么?如果是Vista或更高版本,你是否尝试过-i 1参数?我认为会话0是为Windows服务保留的:http://blogs.technet.com/b/askperf/archive/2007/04/27/application-compatibility-session-0-isolation.aspx