[SOLVED] Autotype from clipboard in remote RDP session

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

[SOLVED] Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 04:45

I have a large script code that i need to autotype in a (webbased) Remote Desktop session. But because the script Tabs someone right before start of a text an extra nummer is typed, e.g.

Start script
5Get-command
2Get-AdUser-identity Dummysusr1
Write-output “This is a test”
Get-AdGroups -name Dummy
Invoke-command -script a-$scriptblock


so why this happends? I try inserting some delay in autohotkey script but so far no success. of course i do not want to remove the tab in each script i’m trying to paste.

The remote session prohibit Paste clipboard thats why i need to make it type every character.
Last edited by Topsport on 27 Nov 2022, 01:15, edited 1 time in total.

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Autotype from clipboard in remote RDP session

Post by mikeyww » 24 Nov 2022, 05:57

Welcome to this AutoHotkey forum!

You can post your AutoHotkey script in a reply below.

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 08:03

Code: Select all

setkeydelay 300
return
return
!y::Send {Text}%Clipboard%
return

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Autotype from clipboard in remote RDP session

Post by mikeyww » 24 Nov 2022, 08:31

This worked in Notepad. Is it what you are trying to achieve?

Code: Select all

Clipboard =
(
Start script
Get-command
Get-AdUser-identity Dummysusr1
Write-output "This is a test"
Get-AdGroups -name Dummy
Invoke-command -script a-$scriptblock
)
!y::SendInput {Text}%Clipboard%

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 08:52

This work in Windows Notepad for me also. But i want to make autotype within a RDP session as explained in my start post.

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 09:03

In Notepad locally works for me also. But Notepad in a remote RDP session not.
I have recorded a short session but do not know how to upload the video. Its an gif file by the way.

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Autotype from clipboard in remote RDP session

Post by mikeyww » 24 Nov 2022, 09:07

When you run the script, activate RDP, and trigger the hotkey, you see something different? If so, I am not sure how to resolve, but some of the other posts about RDP may help you. Sorry I am not more helpful.

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 09:45

I think because of delays in RDP session. Autohotkey tries to rapidly type a lot of info faster than any human can do. But so yeah, local notepad works flawlessly it not Autohotkey fault. I'll try to find more post if someone else has a workaround for this issue. I really need autotype to work in a remote session because my company does not allow copy+paste.

Thanks for the help

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Autotype from clipboard in remote RDP session

Post by mikeyww » 24 Nov 2022, 10:05

Worth a try:

Code: Select all

!y::
SetKeyDelay, 25, 25
Send {Text}%Clipboard%
Return

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 10:57

I tried your code and a lot of codes from others at autohotkey forum but none are consisent. E.g. my first few lines.....

Code: Select all

# https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/powershell-managing-an-outlook-mailbox-with-powershell
Write-Host "**** This is JIT_GetFromOutlookItem-in-SubFolder.ps1" *** -ForegroundColor DarkRed -BackgroundColor Green
Write-Warning "Wait for previous Outlook process to close..."
Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue | Stop-Process -Force #!

Do { Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue ; start-sleep 3 }
    #While ( $Proc = (Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue )  )
    While ( (Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue )  )


************************* result in this:

Code: Select all

://docs.microsoft.com/en-aging* Thirors tion ESS SPACE." }
Wr )  )
        { Write-Output "USELESS SPACE." }
Wr )  )
        { Write-Output "USELESS SPACE." }
Wr )  )
        { Write-Output "USELESS SPACE." }
Wr )  )
        { Write-Output "USELESS SPACE." }
Write-Host "No process Outlook.exe was found. " {#}-NoNewline ; Get-Datespace$mapi = $OutlookApplication.Go the mapi namespace$mapi = $OutlookApplication.GetNamespace("Mapi");

User avatar
pervect66
Posts: 27
Joined: 09 Jun 2021, 06:53

Re: Autotype from clipboard in remote RDP session

Post by pervect66 » 24 Nov 2022, 11:15

I wrote a tool for remote working in an RDP session, and Mikey is right: SetKeyDelay is key. I usually have delay=1, duration=0

The other thing is that if you use the RDP client, Windows key combinations are being executed locally. You can set this up in the RDP client GUI to be sent to the remote session, or edit the .rdp config file and add this line:
keyboardhook:i:0

That makes sure that even in a fullscreen RDP session, the keys are being sent to the RDP session.

My employer is very paranoid, so even copy/paste etc has been disabled. I also use my tool to get around that :o
To be clear: my tool runs on my local machine.

If you need more info: please shoot me a message!

User avatar
pervect66
Posts: 27
Joined: 09 Jun 2021, 06:53

Re: Autotype from clipboard in remote RDP session

Post by pervect66 » 24 Nov 2022, 11:17

I wrote a tool for remote working in an RDP session, and @Mikey is right: SetKeyDelay is key. I usually have delay=1, duration=0

The other thing is that if you use the RDP client, Windows key combinations are being executed locally. You can set this up in the RDP client GUI to be sent to the remote session, or edit the .rdp config file and add this line:
keyboardhook:i:0

That makes sure that even in a fullscreen RDP session, the keys are being sent to the RDP session.

My employer is very paranoid, so even copy/paste etc has been disabled. I also use my tool to get around that :o
To be clear: my tool runs on my local machine.

If you need more info: please shoot me a message!

(PS: Dutch?)

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 11:29

Setting keyboardhook:i:0 in RDP-file seems to make a little better, but not yet there.
The # for remarks and <# #> which we use in Powershell scripts a lot are now typed correctly
The first 50 lines or so of the script are correctly typed (though each line has an extra hard returns in it which the original script does not have)
o { Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue ; start-sleep 3 }

#While ( $Proc = (Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue ) )

While ( (Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue ) )

{ Write-Output ÜSELESS SPACE."}

Write-Host "No process Outlook.exe was found. "#-NoNewline ; Get-Date -UFormat %Y-%m-%d_%H:%M_uur



# Instantiate a new Outlook object

$OutlookApplication = New-Object -ComObject Öutlook.Application";



# Map to the mapi namespace

$mapi = $OutlookApplication.GetNamespace("Mapi");


But after lines 50, things get bit out messy, there are now a lot of spaces added before the actual text beeing typed.

Code: Select all

       else

            {

                Write-Host "Result after second try to find '$TodayDate'...."-NoNewline

                    $TimeinBody = $VerifyBody | Select-String "$TodayDate"

    

            if ($TimeinBody) {Write-Host "Success!"}

                    else {Write-Output No $TodayDate found - abort script;BREAK}    

                        

                            }


And somewhere lines 200 it more like it trying to type in Greeks:
rpung-catupgr_EI t $+EgoT(C sSaeis PEA> LectFREM(older.Fu
I'm now bit closer to the solution...

And yes, i face same problem as you do and i speak Dutch also :)

User avatar
pervect66
Posts: 27
Joined: 09 Jun 2021, 06:53

Re: Autotype from clipboard in remote RDP session

Post by pervect66 » 24 Nov 2022, 11:43

Make sure you save the script as UTF-8 BOM.
Use SendEvent instead of Send, set SetKeyDelay 10,10

Veel plezier

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 13:07

I have implemented all suggestions and besides adjusting RDP-file, Autohotkey code looks like this

Code: Select all

!y::
SetKeyDelay, 10, 10
SendEvent {Raw}%Clipboard%
Return
It is about 97% good. E and A as Ë Ä. As long as it is not 100% i will have a hard time to read the code over again autohotkey finishes... :!: :problem:
And if that it is fixed, next i need to try to fix same problem in a webbased-RDP session. This is the end goal, because in future direct RDP to remote server from my local machine will be prohibited.

:?: By the way, when Autohotkey finishes, my local keyboard response intermittently. CTRL+ALT+DEL resolve the problem. Anyone knows why this happends?

User avatar
pervect66
Posts: 27
Joined: 09 Jun 2021, 06:53

Re: Autotype from clipboard in remote RDP session

Post by pervect66 » 24 Nov 2022, 14:12

Without the code+the clipboard contents there is not much more I can do....

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 24 Nov 2022, 15:39

I spoke too soon. I did not convert to UTF-8 correctly. Now i have and its nearly perfect 99% correct. And for each new line, there is an extra hard returns somehow. But that's ok, i only need this code once as i don't want to save the code on the server.

Now my next project is to make it work in an webbased-RDP session also. And since i can't adust RDP-file e.g. in this case, it gonna be harder to get it work. To be continued.

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Autotype from clipboard in remote RDP session

Post by mikeyww » 24 Nov 2022, 15:56

It's possible that carriage returns are the "extras". You could try deleting them.

User avatar
pervect66
Posts: 27
Joined: 09 Jun 2021, 06:53

Re: Autotype from clipboard in remote RDP session

Post by pervect66 » 24 Nov 2022, 16:02

Topsport wrote:
24 Nov 2022, 15:39
I spoke too soon. I did not convert to UTF-8 correctly. Now i have and its nearly perfect 99% correct. And for each new line, there is an extra hard returns somehow. But that's ok, i only need this code once as i don't want to save the code on the server.

Now my next project is to make it work in an webbased-RDP session also. And since i can't adust RDP-file e.g. in this case, it gonna be harder to get it work. To be continued.
It depends what kind of web-based RDP it is?

And generally, UTF-8 BOM is to be preferred over UTF-8

Topsport
Posts: 14
Joined: 24 Nov 2022, 04:05

Re: Autotype from clipboard in remote RDP session

Post by Topsport » 25 Nov 2022, 00:13

@pervect66 UTF-8 with BOM - got it.

Background: one of my customers will be using Cyberark (PAM) and restrict us to using webbased-RDP to access their servers. And they do not allow copy+paste text from my local workstatoin to the remote server. I have source codes from a lot of different places where normally i can access from my own computer without problem. But when i logon the the remote server, it is actually a shared session. I dont want to leave my cookies for others to grab. And i certainly do not want to leave my credentials in the profile because the next time another person automatically get my session/profile and well the rest you know.

And you guys were asking about my original code, here is a snippet:
My original code looks like this

Code: Select all

Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue | Stop-Process -Force #!

Do { Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue ; start-sleep 3 }
    While ( (Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue )  )
        { Write-Output "USELESS SPACE." }
Write-Host "No process Outlook.exe was found. "

# Instantiate a new Outlook object
$OutlookApplication = New-Object -ComObject "Outlook.Application";

# Map to the mapi namespace
$mapi = $OutlookApplication.GetNamespace("Mapi");

Write-Host   "Wait 5 secs !" -ForegroundColor Red ; Start-Sleep 5 #* 2021-12-24

$TargetMailbox = $mapi.Folders | Where-Object {$_.FolderPath -eq "\\[email protected]" }

$SelectFolder = $TargetMailbox.Folders.item(2).Folders.item(4).Folders.item('myItem') 
$SelectFolder | Select FullFolderPath -ExpandProperty FullFolderPath

$Jit_file = 'C:\Users\myname'+'\export.TXT'
$Jit_temp = 'C:\Users\myname'+'\TEMP_file.TXT'

$TodayDate = Get-Date -UFormat "%m/%d/%Y"
After pasting in a RDP-session it looks like this:

Code: Select all

Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue | Stop-Process -Force #!



Do { Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue ; start-sleep 3 }

    While ( (Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue )  )

            { Write-Output "USELESS SPACE." }

            Write-Host "No process Outlook.exe was found. "



            # Instantiate a new Outlook object

            $OutlookApplication = New-Object -ComObject "Outlook.Application";



            # Map to the mapi namespace

            $mapi = $OutlookApplication.GetNamespace("Mapi");



            Write-Host   "Wait 5 secs !" -ForegroundColor Red ; Start-Sleep 5 #* 2021-12-24



            $TargetMailbox = $mapi.Folders | Where-Object {$_.FolderPath -eq "\\[email protected]" }



            $SelectFolder = $TargetMailbox.Folders.item(2).Folders.item(4).Folders.item('myItem') 

            $SelectFolder | Select FullFolderPath -ExpandProperty FullFolderPath



            $Jit_file = 'C:\Users\myname'+'\export.TXT'
            $Jit_temp = 'C:\Users\myname'+'\TEMP_file.TXT'



            $TodayDate = Get-Date -UFormat "%m/%d/%Y"

and the WEB-RDP version looks like this:

Code: Select all

Write-Warning "ait for previous Outlook process to close..."
Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue | Stop-Process -Force #!

Do { Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue ; start-sleep 3 }
    While ( (Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue )  )
        { Write-Output "USELESS SPACE." }
Write-Host "No process Outlook.exe was found. "

# Instantiate a new Outlook object
$OutlookApplication = New-Object -ComObject "Outlook.Application";

# Map to the mapi namespace
$mapi = $OutlookApplication.GetNamespace("api";

Write-Host   "Wait 5 secs !" -ForegroundColor Red ; Start-Sleep 5 #* 2021-12-24

$TargetMailbox = 4mapi.Folders | Where-Object {-4_.FolderPath -eq "\[email protected]" }

$SelectFolder = 4TargetMailbox.Folders.item(2).Folders.item(4).Folders.item('yItem') 
$SelectFolder | Select FullFolderPath -ExpandProperty FullFolderPath

$Jit_file = '"Ç:\Users\myname'+'\export.TXT'
$Jit_temp = 'Ç:\Users\myname'+'\TEMP_file.TXT'

$TodayDate = Get-Date -UFormat 'm/%d/%Y"
So appearently it has trouble when a $ sign is used in in the middel of the script. the \ ) ' and so on.

Post Reply

Return to “Ask for Help (v1)”