使用psexec在远程机器上运行AutoIt

1 投票
3 回答
3255 浏览
提问于 2025-04-18 17:14

我正在尝试在远程机器上运行一个AutoIt脚本。

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

我想从这个脚本中打开一个记事本,并在里面写点东西。同时,我也在脚本中写一些日志。虽然我能看到日志,但我却看不到屏幕上发生的任何事情。

我试过用参数 -i 0,这个参数可以打开一个交互式的界面,但这和在本地机器上运行脚本的体验不一样。有没有其他方法可以做到这一点呢?

3 个回答

0

你在用什么操作系统?如果是Vista或者更新的版本,你试过加上参数 -i 1 吗?我觉得会话0是留给Windows服务用的:http://blogs.technet.com/b/askperf/archive/2007/04/27/application-compatibility-session-0-isolation.aspx

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

撰写回答