Jump to content

Verify Powershell opens in RDP session


Recommended Posts

Hi,

I'm seeking guidance on the most effective way to detect whether the PowerShell window is open (in an rdp session) before attempting to run additional commands or taking alternative actions.

1)  Launch an RDP session to a client (with only rdp available - firewall blocks other access)  [This portion is working]

2)  Open a powershell and run several commands. [ Sometimes powershell opens sometimes not ]

Not sure the best way to detect that the powershell window is open before running additional commands or know to skip it.

 

Thanks in Advance 

Link to comment
Share on other sites

use psexec (sysinternals) to open a powershell on the destination machine directly.

PsExec.exe \\REMOTEHOSTNAME powershell.exe -accepteula

Note: you will need to have local admin rights on the remote PC.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

  • 2 weeks later...

psexec is not powershell, but a tool to connect to a remote windows host and to launch *THERE* the program, you want to use on the remote host.

psexec is using TCP:135 and TCP:445 (SMB sharing)

 

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Perhaps always trying to close and then open,

Opt('WinWaitDelay', 250)
Opt('WinDetectHiddenText', 1)
Opt('MouseCoordMode', 0)
Opt("WinTitleMatchMode", 1) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
Opt("SendKeyDelay", 75)
Opt("SendKeyDownDelay", Default)
Opt("SendCapslockMode", 1)

Global $hWnd

$hWnd = WinWait("__._._.__ - Conexión a Escritorio remoto", "", 90)
If WinExists($hWnd) Then
    If Not WinActive($hWnd, "") Then
        WinActivate($hWnd)
        WinWaitActive($hWnd, "", 60000)
        ;WinSetState($hWnd, "", @SW_MAXIMIZE)
        Sleep(1000)
    EndIf

    If WinActive($hWnd, "") Then
        ;_FileWriteLog($hFile, "OK Active Window")
    EndIf
EndIf

;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
If ControlSend($hWnd, "", "", "#r") = 1 Then
    ;_FileWriteLog($hFile, "OK ControlSend: #r [" & $iLine & "]")
    Sleep(2000)
    If ControlSend($hWnd, "", "", "TASKKILL /F /IM EXCEL.EXE") = 1 Then
        ;_FileWriteLog($hFile, "OK ControlSend: TASKKILL /F /IM EXCEL.EXE [" & $iLine & "]")
        Sleep(2000)
        ControlSend($hWnd, "", "", "{ENTER}")
    Else
        SetError(2)
        ;_FileWriteLog($hFile, "Error ControlSend: TASKKILL /F /IM EXCEL.EXE [" & $iLine & "]")
    EndIf
Else
    SetError(1)
    ;_FileWriteLog($hFile, "Error ControlSend: #r [" & $iLine & "]")
EndIf
Sleep(2000)

 

Link to comment
Share on other sites

On 10/31/2023 at 8:10 AM, DomoArigato said:

in an rdp session

Throughout the years, very many people has the same kind of question. The answer should be: "RDP is like a video you can click an type", therefore to have anything happening on the remote device, it should run there and not in the local device because: "RDP is like a video you can click an type".

And anyway, anyone with the inability of running a program at will, is simply because is not permitted by the admin/IT/company/mom/dad/???, thereby braking some rule somewhere, maybe not a forum rule, maybe, but something that'll get you in trouble nonetheless.

That been said, I should go back to bed. Is too early here and by brain is not as awake as my fingers are :)

Edited by argumentum
spelling

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

22 hours ago, argumentum said:

by brain is not as awake as my fingers are

There would be common sense rules that we should all respect, for example, avoiding unfounded suspicions/accusations and always thinking carefully before writing.

And these are laws or fundamental rights: the presumption of innocence and the freedom of expression.

Here an example of a broken logical rule:

You drive the company car and you get fined.

There is a company rule that you must pay,
because you should avoid infringement of the rules

But if the fine refers to the condition of the vehicle, the rule does not make any sense in this case.

There's a saying in Spanish:

Prudence is a good advisor

 

Quote

That been said,

I need to interact with the rdp session because of an accounting software that uses flat files instead of a relational database.

I'm using AutoIt as user automation, to export a .rtf report and from this file loading Access tables.

Does this breaks some rule ?

Possibly, because the vendor offers an expensive solution to export your own data to databases.

Edited by robertocm
Link to comment
Share on other sites

9 hours ago, argumentum said:

"RDP is like a video you can click an type"

4 hours ago, robertocm said:

Does this breaks some rule ?

Its quite simple: "RDP is like a video you can click an type". Unless you run your code from within the remote desktop, "RDP is like a video you can click an type".
You'll be limited to whatever an image would allow. As far as forum rules, each time you type forum rules, the forum will add a link to the string.

I hope you understand that am not judging your character, just stating the limitation that, by doing a search in the forum you'll see, there is not much that can be done.

Good luck with your code and if you do in fact find a solution, share the code :)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

12 hours ago, argumentum said:

Good luck with your code and if you do in fact find a solution, share the code

 

#include <File.au3>
#include <Date.au3>
#include <Timers.au3>
#include <WinAPIFiles.au3>
#include <Misc.au3>
#include "\Include\HTTP\HTTP.au3"
#include "\Include\OpenCV\OpenCV_Match\OpenCV-Match_UDF.au3"
#include "\Include\Festivos.au3"

;_Singleton("Mayores_OpenCV", 0)
If _Singleton("Mayores_OpenCV", 1) = 0 Then
    Msgbox(48,"Atención", "Ya se está ejecutando." & @LF & "Esta instancia se cerrará.", 5)
    Exit
EndIf

AutoItWinSetTitle("Mayores_OpenCV")

;Esperar
;https://www.autoitscript.com/forum/topic/182398-how-do-i-gracefully-close-an-autoit-script-that-is-running/
While 1
    If Not WinExists("Diario") Then
        ExitLoop
    EndIf
    Sleep(1000)
WEnd

If _Festivos() Then
    ;ConsoleWrite("Festivo" & @CRLF)
    Exit
EndIf

Opt('WinWaitDelay', 250)
Opt('WinDetectHiddenText', 1)
Opt('MouseCoordMode', 0)
Opt("WinTitleMatchMode", 1) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
Opt("SendKeyDelay", 75)
Opt("SendKeyDownDelay", Default)
Opt("SendCapslockMode", 1)

Opt("MustDeclareVars", 1)
Opt("TrayIconDebug", 1)
;Be sure to register this to reenable power-saving, screensaver, etc
OnAutoItExitRegister("OnAutoItExit")

#===== CONFIG =====
Global $mouse, $offset, $iFileExists, $hWnd, $Found, $sFilePath, $sFilePath2, $sFilePath4, $sOpenCVDir, $hFile, $YearA3, $FromC, $ToC, $FromF, $ToF, $Puntear, $Saldos, $sms, $CleanLog, $CheckActive, $RestoreBreak
$CheckActive = True
;~ $CheckActive = False
$sms = False
Global Const $Year = @YEAR
Global Const $Mon = @MON ;StringFormat("%02i", @MON - 1)
Global Const $Day = @MDAY
If $CmdLine[0] = 1 Then
    $YearA3 = $Year - 1
Else
    $YearA3 = $Year
EndIf
$Puntear = True
$Saldos = True
$FromF = "01/01/" & $YearA3
$ToF = "31/12/" & $YearA3
$FromC = "4000000001"
$ToC = "4300999999"

$sOpenCVDir = "C:\.........\OpenCV\OpenCV_Match"
$sFilePath = "\\...SERVERNAME...\...\EXP\switch"
Global $sFilePathLog = @DesktopDir & "\Events.log"
$hFile = FileOpen($sFilePathLog, 2)

$sFilePath2 = "\\...SERVERNAME...\...\EXP\tmp.rtf"
$sFilePath4 = "C:\Users\...\mayores.rtf"

#===== OpenCV-Match_UDF =====
;Desde tarea programada de Windows daba error: Failed loading DLLs
FileChangeDir($sOpenCVDir)
_OpenCV_Startup();loads opencv DLLs
_OpenCV_EnableLogging(True, True, True) ;Logs matches, errors in a log file and autoit console output.

If $CheckActive Then
    ;Comprobar que o usuario está inactivo
    ;https://stackoverflow.com/questions/3867584/autoit-how-to-get-system-idle-time-or-if-screensaver-is-active
    ;How to get system idle time, or if screensaver is active?
    ;The _Timer_GetIdleTime() function uses GetLastInputInfo from user32.dll.
    While 1 ;use infinite loop since ExitLoop will get called
        If _DateDiff('n', _NowCalc(), @YEAR & "/" & @MON & "/" & @MDAY & " 20:10:00") > 10 Then
            If _Timer_GetIdleTime() > 600000 Then ;600000
                ExitLoop
            EndIf
        Else
            If _Timer_GetIdleTime() > 300000 Then ;300000
                ExitLoop
            EndIf
        EndIf
        Sleep(10000)
    WEnd
    ;Otra forma
    ;Do
        ;Sleep(10000)
    ;Until _Timer_GetIdleTime() > 300000
EndIf

If _DateDiff('n', _NowCalc(), $Year & "/" & $Mon & "/" & $Day & " 20:16:00") > 1 Then $ToC = "5230999999"

;Introducir pausa: conexión de rede pode tardar en restaurarse cando equipo estaba suspendido
For $i = 0 To 30
    If FileExists("\\...SERVERNAME...\...\EXP") Then
        ExitLoop
    Else
        If $i < 30 Then
            Sleep(5000)
        Else
            _FileWriteLog($hFile, "Error Acceso a carpeta ...\EXP")
            Exit
        EndIf
    EndIf
Next

If ProcessExists("thunderbird.exe") Then ; Check if the thunderbird process is running.
    ProcessClose("thunderbird.exe")
EndIf
If ProcessExists("EXCEL.exe") Then ; Check if the thunderbird process is running.
    ProcessClose("EXCEL.exe")
EndIf
If ProcessExists("mstsc.exe") Then ;Check if the process is running.
    ProcessClose("mstsc.exe")
EndIf

;Pode estar suspendido
MouseMove(@DesktopWidth - 100, @DesktopHeight -  100)
Sleep(20000)
Send("#d")

;https://www.autoitscript.com/forum/topic/97501-auto-mstsc/
;RunWait('"' & @ComSpec & '" /c MSTSC.EXE C:/yourfile.rdp', '', @SW_HIDE)
;http://support.microsoft.com/kb/941641
Run("C:\windows\system32\mstsc.exe /v:" & "__._._._")

$hWnd = WinWait("Conexión a Escritorio remoto", "Errores de certificado", 10)
If WinExists($hWnd) Then
    If Not WinActive($hWnd, "") Then
        WinActivate($hWnd)
        WinWaitActive($hWnd, "", 5)
        ;WinSetState($hWnd, "", @SW_MAXIMIZE)
        Sleep(1000)
    EndIf

    If WinActive($hWnd, "") Then
        If ControlSend($hWnd, "", "", "s") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: s [Errores Certificado]")
        Else
            _FileWriteLog($hFile, "Error ControlSend: s [Errores Certificado]")
            Exit
        EndIf
        Sleep(5000)
    EndIf
EndIf

;Wait 90 seconds for the window to appear.
$hWnd = WinWait("__._._._ - Conexión a Escritorio remoto", "", 90)
If WinExists($hWnd) Then
    If Not WinActive($hWnd, "") Then
        WinActivate($hWnd)
        WinWaitActive($hWnd, "", 60000)
        ;WinSetState($hWnd, "", @SW_MAXIMIZE)
        Sleep(1000)
    EndIf

    If WinActive($hWnd, "") Then
        _FileWriteLog($hFile, "OK Ventana Activa: Conexión a Escritorio")
    EndIf
EndIf

;Test if the window is now activated
;You can use the WinActive() function to check if WinActivate() succeeded.
If WinActive($hWnd, "") Then
    ;WinSetState($hWnd, "", @SW_MAXIMIZE)
    ;MsgBox(0, "", "Window NOW active")

    ;DirCreate($sFilePath3)

    ;por se quedou dunha execución anterior
    FileRecycle($sFilePath)
    FileRecycle($sFilePath2)

    MouseMove(@DesktopWidth - 80, @DesktopHeight -  80)
    Sleep(10000)

    Global $sCoords[4] = [1416, 646, 1418, 648]
    Global $count, $iCheckSum = 1769473 ;black screen
    $offset = 1 ; how far to move the mouse
    ;Get initial checksum we will check later
    ;$iCheckSum = PixelChecksum($sCoords[0], $sCoords[1], $sCoords[2], $sCoords[3])
    ;ConsoleWrite($iCheckSum & @CRLF)
    ;Wait for the region to change, the region is checked every 5000ms to reduce CPU load
    While $iCheckSum = PixelChecksum($sCoords[0], $sCoords[1], $sCoords[2], $sCoords[3])
        Sleep(5000)
        $mouse = MouseGetPos()
        MouseMove($mouse[0] + $offset, $mouse[1] + $offset)
        $offset = -$offset
        $count += 1
        If $count = 1800 Then
            _FileWriteLog($hFile, "ExitLoop: Black Screen $count = 1800")
            ExitLoop
            ;_ScreenCapture_Capture($sFilePath3 & $Day & @HOUR & @MIN & ".png" , $sCoords[0], $sCoords[1], $sCoords[2], $sCoords[3], False)
        EndIf
    WEnd
    ;MsgBox(0, "", "Something in the region has changed!")
    Sleep(10000)

    _CheckFullScreen()
    _ProcessClean(@ScriptLineNumber)
    Sleep(5000)

    ;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
    If ControlSend($hWnd, "", "", "#r") = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: #r")
        Sleep(2000)
        If ControlSend($hWnd, "", "", "C:\a3\A3CONV5\COWMENU.EXE") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: command")
            Sleep(2000)
            ControlSend($hWnd, "", "", "{ENTER}")
        Else
            _FileWriteLog($hFile, "Error ControlSend: command")
            Exit
        EndIf
    Else
        _FileWriteLog($hFile, "Error ControlSend: #r")
        Exit
    EndIf

    Sleep(7000)

    $count = 0
    For $i = 1 To 10
        $count += 1
        $offset = 1 ; how far to move the mouse
        $mouse = MouseGetPos()
        MouseMove($mouse[0] + $offset, $mouse[1] + $offset)
        $offset = -$offset
        Sleep(1000)
        MouseMove($mouse[0] + $offset, $mouse[1] + $offset)

        ;Please note that these examples might not work as the match pictures have to be found with the exact same size on your screen.
        Global $sCoords[4] = [770, 470, 778, 481]
        Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\01.png", 0.70, $sCoords, 30, 1000);Try to find the match picture on the screen. Number of tries: 60, Sleep between each try: 500ms.
        If Not @error Then
            _MarkMatch($Match1) ;Debugging: Draws a rect on the screen/coordinates of the match to show the user where the match was found
            Sleep(2000)

            MouseClick($MOUSE_CLICK_LEFT, 747, 440)
            Sleep(1000)
            If ControlSend($hWnd, "", "", "ABCDEFG") = 1 Then
                _FileWriteLog($hFile, "OK ControlSend: ABCDEFG")
            Else
                _FileWriteLog($hFile, "Error ControlSend: ABCDEFG")
                Exit
            EndIf
            Sleep(1000)

            _ClickMouse($Match1, "left",1) ;Calculates the center of the match and clicks the left mouse once on click position
            Sleep(1000)
            ExitLoop
        Else
            If $count = 10 Then
                _FileWriteLog($hFile, "No match found: " & $sOpenCVDir & "\Match\a3\01.png")
                Exit
            EndIf
        EndIf
    Next

    Sleep(5000)
    ;Acceso a aplicaciones: a3con
    Global $sCoords[4] = [302, 238, 365, 276]
    Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\01b.png", 0.70, $sCoords, 60, 1000);Try to find the match picture on the screen. Number of tries: 60, Sleep between each try: 500ms.
    If Not @error Then
        _MarkMatch($Match1) ;Debugging: Draws a rect on the screen/coordinates of the match to show the user where the match was found
        Sleep(100)
        _ClickMouse($Match1, "left",1) ;Calculates the center of the match and clicks the left mouse once on click position
        Sleep(1000)
    Else
        _FileWriteLog($hFile, "No match found: " & $sOpenCVDir & "\Match\a3\01b.png")
        Exit
    EndIf
    Sleep(10000)

    $count = 0
    For $i = 1 To 10
        $count += 1
        $offset = 1 ; how far to move the mouse
        $mouse = MouseGetPos()
        MouseMove($mouse[0] + $offset, $mouse[1] + $offset)
        $offset = -$offset
        Sleep(1000)
        MouseMove($mouse[0] + $offset, $mouse[1] + $offset)

        Global $sCoords[4] = [268, 91, 276, 100]
        Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\02.png", 0.70, $sCoords, 30, 1000);Try to find the match picture on the screen. Number of tries: 10, Sleep between each try: 500ms.
        If Not @error Then
            _MarkMatch($Match1) ;Debugging: Draws a rect on the screen/coordinates of the match to show the user where the match was found
            Sleep(100)

            For $i = 0 To 1
                If ControlSend($hWnd, "", "", $Day & $Mon & $YearA3) = 1 Then
                    Sleep(500)
                    If ControlSend($hWnd, "", "", "{TAB}") = 1 Then
                        Sleep(200)
                        _FileWriteLog($hFile, "OK ControlSend: {TAB}")
                    Else
                        _FileWriteLog($hFile, "Error ControlSend: {TAB}")
                        Exit
                    EndIf
                    _FileWriteLog($hFile, "OK ControlSend: " & $Day & $Mon & $YearA3)
                Else
                    _FileWriteLog($hFile, "Error ControlSend: " & $Day & $Mon & $YearA3)
                    Exit
                EndIf
            Next
            Sleep(1000)

            _ClickMouse($Match1, "left",1) ;Calculates the center of the match and clicks the left mouse once on click position
            Sleep(1000)
            ExitLoop
        Else
            If $count = 10 Then
                _FileWriteLog($hFile, "No match found: " & $sOpenCVDir & "\Match\a3\02.png")
                Exit
            EndIf
        EndIf
    Next

    Global $sCoords[4] = [1047, 371, 1057, 378]
    Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\03.png", 0.70, $sCoords, 30, 1000);Try to find the match picture on the screen. Number of tries: 10, Sleep between each try: 500ms.
    If Not @error Then
        _MarkMatch($Match1) ;Debugging: Draws a rect on the screen/coordinates of the match to show the user where the match was found
        Sleep(100)

        ;Avisos con pouco texto pode estar visible ainda
        Global $sCoords[4] = [1046, 355, 1328, 382]
        While 1 ;use infinite loop since ExitLoop will get called
            Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\03b.png", 0.70, $sCoords, 1, 1000);Try to find the match picture on the screen. Number of tries: 10, Sleep between each try: 500ms.
            If @error Then
                Sleep(100)
                ExitLoop
            EndIf
            Sleep(1000)
        WEnd

        If ControlSend($hWnd, "", "", "{F10}lcmi") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: {F10}lcmi")
        Else
            _FileWriteLog($hFile, "Error ControlSend: {F10}lcmi")
            Exit
        EndIf
        Sleep(1000)

        ;_ClickMouse($Match1, "left",1) ;Calculates the center of the match and clicks the left mouse once on click position
        ;Sleep(1000)
    Else
        _FileWriteLog($hFile, "No match found: " & $sOpenCVDir & "\Match\a3\03b.png")
        Exit
    EndIf
    Sleep(1000)

    Global $sCoords[4] = [33, 138, 41, 145]
    Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\mayores_01.png", 0.70, $sCoords, 30, 1000);Try to find the match picture on the screen. Number of tries: 10, Sleep between each try: 500ms.
    If Not @error Then
        _MarkMatch($Match1) ;Debugging: Draws a rect on the screen/coordinates of the match to show the user where the match was found
        Sleep(100)
        _ClickMouse($Match1, "left",1) ;Calculates the center of the match and clicks the left mouse once on click position
        Sleep(1000)
    Else
        _FileWriteLog($hFile, "No match found: " & $sOpenCVDir & "\Match\a3\mayores_01.png")
        Exit
    EndIf

    If WinActive($hWnd, "") Then
        MouseClick($MOUSE_CLICK_LEFT, 118, 180)
        _FileWriteLog($hFile, "Click")
        Sleep(1000)
    EndIf

    If ControlSend($hWnd, "", "", $FromF) = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: " & $FromF)
    Else
        _FileWriteLog($hFile, "Error ControlSend: " & $FromF)
        Exit
    EndIf
    Sleep(1000)

    If ControlSend($hWnd, "", "", "{TAB}") = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: {TAB}")
    Else
        _FileWriteLog($hFile, "Error ControlSend: {TAB}")
        Exit
    EndIf
    Sleep(500)

    If ControlSend($hWnd, "", "", $ToF) = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: " & $ToF)
    Else
        _FileWriteLog($hFile, "Error ControlSend: " & $ToF)
        Exit
    EndIf
    Sleep(1000)

    If ControlSend($hWnd, "", "", "{TAB}") = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: {TAB}")
    Else
        _FileWriteLog($hFile, "Error ControlSend: {TAB}")
        Exit
    EndIf
    Sleep(500)

    If ControlSend($hWnd, "", "", $FromC) = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: " & $FromC)
    Else
        _FileWriteLog($hFile, "Error ControlSend: " & $FromC)
        Exit
    EndIf
    Sleep(1000)

    If ControlSend($hWnd, "", "", "{TAB}") = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: {TAB}")
    Else
        _FileWriteLog($hFile, "Error ControlSend: {TAB}")
        Exit
    EndIf
    Sleep(500)

    If ControlSend($hWnd, "", "", $ToC) = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: " & $ToC)
    Else
        _FileWriteLog($hFile, "Error ControlSend: " & $ToC)
        Exit
    EndIf
    Sleep(1000)

    If ControlSend($hWnd, "", "", "{TAB}") = 1 Then
        _FileWriteLog($hFile, "OK ControlSend: {TAB}")
    Else
        _FileWriteLog($hFile, "Error ControlSend: {TAB}")
        Exit
    EndIf
    Sleep(500)

    If WinActive($hWnd, "") Then
        MouseClick($MOUSE_CLICK_LEFT, 328, 283)
        _FileWriteLog($hFile, "Click: Ambos")
        Sleep(1000)
    EndIf

    ;32 - Listar Nº Referencia
    Global $sCoords[4] = [323, 321, 333, 331]
    Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\05.png", 0.70, $sCoords, 1, 1000);Try to find the match picture on the screen. Number of tries: 10, Sleep between each try: 500ms.
    If Not @error Then
        _MarkMatch($Match1) ;Debugging: Draws a rect on the screen/coordinates of the match to show the user where the match was found
        Sleep(100)
        _ClickMouse($Match1, "left",1) ;Calculates the center of the match and clicks the left mouse once on click position
        Sleep(500)
    ;Else
        ;Exit
    EndIf

    ;34 - No listar ctas sin movimiento
    Global $sCoords[4] = [323, 364, 333, 374]
    Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\05.png", 0.70, $sCoords, 1, 1000);Try to find the match picture on the screen. Number of tries: 10, Sleep between each try: 500ms.
    If Not @error Then
        _MarkMatch($Match1) ;Debugging: Draws a rect on the screen/coordinates of the match to show the user where the match was found
        Sleep(100)
        _ClickMouse($Match1, "left",1) ;Calculates the center of the match and clicks the left mouse once on click position
        Sleep(500)
    ;Else
        ;Exit
    EndIf

    ;35 - No listar ctas con saldo 0
    Global $sCoords[4] = [323, 385, 334, 398]
    Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\06.png", 0.70, $sCoords, 1, 500);Try to find the match picture on the screen. Number of tries: 10, Sleep between each try: 500ms.
    If Not @error Then
        _MarkMatch($Match1) ;Debugging: Draws a rect on the screen/coordinates of the match to show the user where the match was found
        Sleep(100)
        _ClickMouse($Match1, "left",1) ;Calculates the center of the match and clicks the left mouse once on click position
        Sleep(500)
    ;Else
        ;Exit
    EndIf

    If WinActive($hWnd, "") Then
        MouseClick($MOUSE_CLICK_LEFT, 93, 87)
        _FileWriteLog($hFile, "Click: pantalla")
        Sleep(1000)
    EndIf

    $offset = 1
    While 1
        If Not WinExists("__._._._ - Conexión a Escritorio remoto") Then
            _FileWriteLog($hFile, "Error Not WinExists: __._._._")
            ExitLoop
        Else
            If Not WinActive($hWnd, "") Then
                If _Timer_GetIdleTime() > 30000 Then
                    WinActivate($hWnd)
                    WinWaitActive($hWnd, "", 10)
                    WinSetState($hWnd, "", @SW_MAXIMIZE)
                EndIf
                Sleep(5000)
            Else
                If $Found = False Then
                    Global $sCoords[4] = [132, 45, 139, 55]
                    Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\Listado_Plan_Contable.png", 0.70, $sCoords, 1, 1000);Try to find the match picture on the screen. Number of tries: 1, Sleep between each try: 1000ms.
                    If @error Then
                        If WinActive($hWnd, "") Then
                            $mouse = MouseGetPos()
                            MouseMove($mouse[0] + $offset, $mouse[1] + $offset)
                            $offset = -$offset
                        EndIf
                    Else
                        $Found = True
                        ConsoleWrite("Found: Listado_Plan_Contable.png" & @crlf)
                        _FileWriteLog($hFile, "Found: Listado_Plan_Contable.png")
                    EndIf
                    Sleep(5000)
                Else
                    Global $sCoords[4] = [132, 45, 139, 55]
                    Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\a3\Listado_Plan_Contable.png", 0.70, $sCoords, 1, 1000);Try to find the match picture on the screen. Number of tries: 1, Sleep between each try: 1000ms.
                    If @error Then
                        _FileWriteLog($hFile, "Not Found: Listado_Plan_Contable.png")
                        ConsoleWrite("Not Found: Listado_Plan_Contable.png" & @crlf)

                        If ControlSend($hWnd, "", "", "{F10}") = 1 Then
                            _FileWriteLog($hFile, "OK ControlSend: {F10}")
                        Else
                            _FileWriteLog($hFile, "Error ControlSend: {F10}")
                            Exit
                        EndIf
                        Sleep(3000)
                        If ControlSend($hWnd, "", "", $sFilePath2) = 1 Then
                            _FileWriteLog($hFile, "OK ControlSend: " & $sFilePath2)
                        Else
                            _FileWriteLog($hFile, "Error ControlSend: " & $sFilePath2)
                            Exit
                        EndIf
                        Sleep(3000)
                        If ControlSend($hWnd, "", "", "{ENTER}") = 1 Then
                            _FileWriteLog($hFile, "OK ControlSend: {ENTER}")
                        Else
                            _FileWriteLog($hFile, "Error ControlSend: {ENTER}")
                            Exit
                        EndIf
                        For $j = 0 To 29
                            Sleep(20000)
                            If WinActive($hWnd, "") Then
                                $mouse = MouseGetPos()
                                MouseMove($mouse[0] + $offset, $mouse[1] + $offset)
                                $offset = -$offset
                            EndIf
                            $iFileExists = FileExists($sFilePath2)
                            If $iFileExists Then
                                _FileWriteLog($hFile, "OK FileExists: " & $sFilePath2)
                                ExitLoop
                            Else
                                _FileWriteLog($hFile, "Sleep(20000)")
                            EndIf
                        Next
                        ExitLoop
                    Else
                        Sleep(5000)
                    EndIf
                EndIf
            EndIf
        EndIf
    WEnd

    For $j = 0 To 300
        $iFileExists = FileExists($sFilePath2)
        If $iFileExists Then
            While _WinAPI_FileInUse($sFilePath2)
               Sleep(1000)
            WEnd
            ExitLoop
        Else
            Sleep(1000)
        EndIf
    Next

    If FileExists($sFilePath2) Then
        If FileMove($sFilePath2, $sFilePath4, 1) = 1 Then
            _FileWriteLog($hFile, "OK FileMove")
            Global $iFileSize = FileGetSize($sFilePath4)
            $CleanLog = True
        Else
            _FileWriteLog($hFile, "Error FileMove")
            Exit
        EndIf

        Sleep(1000)
        While _WinAPI_FileInUse($sFilePath4)
            Sleep(1000)
        WEnd
        Sleep(1000)

        ;Evitar coincidir con ... (22 horas)
        While 1 ;use infinite loop since ExitLoop will get called
            If Abs(_DateDiff('n', $Year & "/" & $Mon & "/" & $Day & " 22:00:00", _NowCalc())) > 5 Then
                ExitLoop
            EndIf
            Sleep(1000)
        WEnd
        ;Outra forma
        ;Do
            ;Sleep(1000)
        ;Until Abs(_DateDiff('n', @YEAR & "/" & $Mon & "/" & @MDAY & " 22:00:00", _NowCalc())) > 5

        RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & "C:\Users\...\Strings\RTF_to_DataBase.au3" & '"', "", @SW_SHOW, 0)
        _FileWriteLog($hFile, "OK: RTF_to_DataBase.au3")

        FileRecycle($sFilePath4)
        _FileWriteLog($hFile, "FileRecycle")

        If $sms Then
            Global $sResp = _HTTP_Post("http://api.mensatek.com/sms/v5/enviar.php", "Correo=" & URLEncode("SOME@DOMAIN.com") & "&Passwd=01234&Remitente=ABCDE&Destinatarios=" & URLEncode("34612345678") & "&Mensaje=" & URLEncode("OK: File size " & $iFileSize & " bytes") & "&Resp=JSON")

            ;Open the file for write access.
            Global $hFileOpen = FileOpen(@DesktopDir & "\Resp_mensatek.txt", 2)
            ;If $hFileOpen = -1 Then
             ;MsgBox(0, "", "An error occurred when reading/writing the file.")
            ;EndIf
            ;Write some data.
            FileWrite($hFileOpen, $sResp)
            ;Close the handle returned by FileOpen.
            FileClose($hFileOpen)
        EndIf
    Else
        _FileWriteLog($hFile, "Error Not FileExists: " & $sFilePath2)
        Exit
    EndIf
Else
    ;MsgBox(0, "Error", "Window not active")
    ;Local $hActive = WinGetHandle("[active]")
    ;Local $sTitle = WinGetTitle("[active]")
    ;MsgBox(0, "Error", $sTitle)

    ;Open the file for write access.
    Global $hFileOpen = FileOpen(@DesktopDir & "\RDP_A3_OpenCV_ERROR.txt", 2)
    ;If $hFileOpen = -1 Then
    ;MsgBox(0, "", "An error occurred when reading/writing the file.")
    ;EndIf
    ;Write some data.
    FileWrite($hFileOpen, "ERROR: Conexión a Escritorio remoto non é a ventana activa")
    ;Close the handle returned by FileOpen.
    FileClose($hFileOpen)
EndIf

Func _CheckFullScreen()
    ;Check fullscreen
    ;https://superuser.com/questions/94620/remote-desktop-doesnt-maximize-to-fullscreen
        ;Try CTRL+ALT+BREAK - which switches the client between full-screen mode and window mode.
        ;https://learn.microsoft.com/en-us/windows/win32/termserv/terminal-services-shortcut-keys
        ;---
        ;When you launch RDP dialog and before you logon/connect to the remote computer, select the Display Tab and set your display configuration.
        ;If you want full screen mode, set the slider all the way to the right.
    Global $sCoords[4] = [10, 10, 40, 170]
    While 1 ;use infinite loop since ExitLoop will get called
        Global $Match1 = _MatchPicture($sOpenCVDir & "\Match\0000.png", 0.70, $sCoords, 1, 1000);Try to find the match picture on the screen. Number of tries: 10, Sleep between each try: 500ms.
        If @error Then
            Sleep(100)
            ExitLoop
        EndIf
        ;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
        If ControlSend($hWnd, "", "", "^!{BREAK}") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: ^!{BREAK}")
        Else
            _FileWriteLog($hFile, "Error ControlSend: ^!{BREAK}")
        EndIf
        $RestoreBreak = True
        Sleep(2000)
    WEnd
EndFunc

Func _RDPClose($iLine)
    If Not WinActive($hWnd, "") Then
        WinActivate($hWnd)
        WinWaitActive($hWnd, "", 10)
        WinSetState($hWnd, "", @SW_MAXIMIZE)
        Sleep(3000)
    EndIf

    ;Test if the window is now activated
    ;You can use the WinActive() function to check if WinActivate() succeeded.
    If WinActive($hWnd, "") Then
        ;WinSetState($hWnd, "", @SW_MAXIMIZE)
        ;MsgBox(0, "", "Window NOW active")
        Sleep(2000)

        _CheckFullScreen()

        ;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
        If ControlSend($hWnd, "", "", "#r") = 1 Then
            ;_FileWriteLog($hFile, "OK ControlSend: #r [" & $iLine & "]")
            Sleep(2000)
            If ControlSend($hWnd, "", "", "shutdown -L") = 1 Then
                ;_FileWriteLog($hFile, "OK ControlSend: shutdown -L [" & $iLine & "]")
                Sleep(2000)
                ControlSend($hWnd, "", "", "{ENTER}")
            Else
                SetError(2)
                ;_FileWriteLog($hFile, "Error ControlSend: shutdown -L [" & $iLine & "]")
            EndIf
        Else
            SetError(1)
            ;_FileWriteLog($hFile, "Error ControlSend: #r [" & $iLine & "]")
        EndIf
        Sleep(5000)
    EndIf
EndFunc

Func _ProcessClean($iLine)
    If Not WinActive($hWnd, "") Then
        WinActivate($hWnd)
        WinWaitActive($hWnd, "", 10)
        WinSetState($hWnd, "", @SW_MAXIMIZE)
        Sleep(3000)
    EndIf

    ;Test if the window is now activated
    ;You can use the WinActive() function to check if WinActivate() succeeded.
    If WinActive($hWnd, "") Then
        ;WinSetState($hWnd, "", @SW_MAXIMIZE)
        ;MsgBox(0, "", "Window NOW active")
        Sleep(2000)

        ;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
        If ControlSend($hWnd, "", "", "#r") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: #r [" & $iLine & "]")
            Sleep(2000)
            If ControlSend($hWnd, "", "", "TASKKILL /F /IM A3_SOME-2.EXE") = 1 Then
                _FileWriteLog($hFile, "OK ControlSend: TASKKILL /F /IM A3_SOME-2.EXE [" & $iLine & "]")
                Sleep(2000)
                ControlSend($hWnd, "", "", "{ENTER}")
            Else
                SetError(2)
                _FileWriteLog($hFile, "Error ControlSend: TASKKILL /F /IM A3_SOME-2.EXE [" & $iLine & "]")
            EndIf
        Else
            SetError(1)
            _FileWriteLog($hFile, "Error ControlSend: #r [" & $iLine & "]")
        EndIf
        Sleep(5000)

        ;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
        If ControlSend($hWnd, "", "", "#r") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: #r [" & $iLine & "]")
            Sleep(2000)
            If ControlSend($hWnd, "", "", "TASKKILL /F /IM A3_SOME.EXE") = 1 Then
                _FileWriteLog($hFile, "OK ControlSend: TASKKILL /F /IM A3_SOME.EXE [" & $iLine & "]")
                Sleep(2000)
                ControlSend($hWnd, "", "", "{ENTER}")
            Else
                SetError(2)
                _FileWriteLog($hFile, "Error ControlSend: TASKKILL /F /IM A3_SOME.EXE [" & $iLine & "]")
            EndIf
        Else
            SetError(1)
            _FileWriteLog($hFile, "Error ControlSend: #r [" & $iLine & "]")
        EndIf
        Sleep(5000)

        ;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
        If ControlSend($hWnd, "", "", "#r") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: #r [" & $iLine & "]")
            Sleep(2000)
            If ControlSend($hWnd, "", "", "TASKKILL /F /IM EXCEL.EXE") = 1 Then
                _FileWriteLog($hFile, "OK ControlSend: TASKKILL /F /IM EXCEL.EXE [" & $iLine & "]")
                Sleep(2000)
                ControlSend($hWnd, "", "", "{ENTER}")
            Else
                SetError(2)
                _FileWriteLog($hFile, "Error ControlSend: TASKKILL /F /IM EXCEL.EXE [" & $iLine & "]")
            EndIf
        Else
            SetError(1)
            _FileWriteLog($hFile, "Error ControlSend: #r [" & $iLine & "]")
        EndIf
        Sleep(2000)

        ;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
        If ControlSend($hWnd, "", "", "#r") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: #r [" & $iLine & "]")
            Sleep(2000)
            If ControlSend($hWnd, "", "", "TASKKILL /F /IM SOME.EXE") = 1 Then
                _FileWriteLog($hFile, "OK ControlSend: TASKKILL /F /IM SOME.EXE [" & $iLine & "]")
                Sleep(2000)
                ControlSend($hWnd, "", "", "{ENTER}")
            Else
                SetError(2)
                _FileWriteLog($hFile, "Error ControlSend: TASKKILL /F /IM SOME.EXE [" & $iLine & "]")
            EndIf
        Else
            SetError(1)
            _FileWriteLog($hFile, "Error ControlSend: #r [" & $iLine & "]")
        EndIf
        Sleep(2000)

        ;https://stackoverflow.com/questions/26505329/autoit-controlsend-remote-desktop
        If ControlSend($hWnd, "", "", "#d") = 1 Then
            _FileWriteLog($hFile, "OK ControlSend: #d [" & $iLine & "]")
        Else
            SetError(1)
            _FileWriteLog($hFile, "Error ControlSend: #d [" & $iLine & "]")
        EndIf
        Sleep(2000)
    EndIf
EndFunc

Func OnAutoItExit()
    If WinExists("__._._._ - Conexión a Escritorio remoto") Then
        If Not WinActive($hWnd, "") Then
            WinActivate($hWnd)
            WinWaitActive($hWnd, "", 10)
            WinSetState($hWnd, "", @SW_MAXIMIZE)
            Sleep(5000)
        EndIf

        ;Test if the window is now activated
        ;You can use the WinActive() function to check if WinActivate() succeeded.
        If WinActive($hWnd, "") Then
            ;WinSetState($hWnd, "", @SW_MAXIMIZE)
            ;MsgBox(0, "", "Window NOW active")
            _RDPClose(@ScriptLineNumber)
        EndIf
    EndIf

    _OpenCV_Shutdown();Closes DLLs
    ;_CFS_ReleaseSemaphore($logfile)
    FileClose($hFile)
    If $CleanLog Then FileRecycle($sFilePathLog)

    If $RestoreBreak Then
        ;google keywords: restore pause break key vba
        ;https://stackoverflow.com/questions/2154699/excel-vba-app-stops-spontaneously-with-message-code-execution-has-been-halted
        Send("{CTRLDOWN}{BREAK}{CTRLUP}")
    EndIf
EndFunc

 

Include.7z

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