Is there a way to programatically copy and paste to an RDP?

12,617

Solution 1

You can use PowerShell to put file to clipboard:

Add-Type -AssemblyName System.Windows.Forms
$f = New-Object System.Collections.Specialized.StringCollection
$f.Add("D:\tmp\mypic1.bmp")
[System.Windows.Forms.Clipboard]::SetFileDropList($f)

Solution 2

If you intend to stay connected to the RDP session, then you can map a drive on your connection which can make your local drive available to the remote computer. This is on Remote Desktop's Local Resources tab under the More... button.

Once that is enabled, you can make a scheduled task to copy files. You can access the local drives from the special share name \\tsclient\c or whichever drive you are trying to access, as described in this TechNet article. So a sample script that you would run on the remote computer might look something like this:

IF EXIST "\\tsclient\c\Mydir\MyFile" copy /Y "\\tsclient\c\Mydir\MyFile" "C:\Syncrhonized\MyDir\MyFile"

You could put this in as a scheduled task to run every few minutes or so. Be aware of a potential problem if other people remote in to this computer with drive mappings and file structure. Your script won't know which computer is the right one, unless you throw some sort of logic in to check that.

Solution 3

As you say that you cannot share your local hard disk with the server, the only sharing mechanism that is left is the clipboard.

If the clipboard is shareable over the RDP connection, you may use the open-source project Clipboard RDC.

This project consists of a very small Java program that is launched on both sides of the connection. The client-side program is used to encode a file and load it to the clipboard, while the server-side program decodes and stores it in a local file. The limitation here is that the file must be small enough to fit on the clipboard.

As it stands, this is more work than you wanted to do. But you can modify this simple program and separate it into two parts :

  1. A client-side program that will accept a file-path as parameter and place the encoded file on the clipboard.
  2. A server-side program that will loop, testing every few seconds for the presence of such a file on the clipboard, decoding and storing it in the folder.

As these will be your own programs that you fully control, you could add features such as adding the destination path to the information that you place on the clipboard, so that this becomes a general-purpose tool for automatically transferring multiple small-enough files from client to server.

Solution 4

According to my research rdpclip.exe doesn't accept any command line arguments. To use it in order to programmatically copy files is apparently not intended.

Since Windows Vista robocopy is part of every Windows installation. I don't know how you wanna use rdpclip.exe to transfer a file to the remote host. But my first thought was to mount a netshare, use robocopy to transfer a file and dismount the netshare. In an script it will look like:

net use \\Server\Sharename /USER:[username] [passwort]
robocopy [Source] [Destination]
net use \\Servername\Sharename /del
Share:
12,617

Related videos on Youtube

Matthew Peters
Author by

Matthew Peters

"I study [cyber]-politics and [cyber]-war that my sons may have liberty to study mathematics and philosophy." - John Adams [modernized]

Updated on September 18, 2022

Comments

  • Matthew Peters
    Matthew Peters over 1 year

    I am looking to write a quick script to send one file (that I change often) from my workstation (windows7 OS) to a remote server (windows server 2012) via the RDP client.

    Ideally, I am looking to instantiate the rdpclip.exe process with some arguments. Something like: rdpclip.exe -f [fileToCopyFromLocalToRemote] but to my knowledge (and a brief google search) there is no such equivalent.

    Also, I am not looking for any third party tools to do this!

  • Kinnectus
    Kinnectus over 8 years
    I was just writing the similar but you beat me to it. The problem the user has is that you can't map a server drive locally via RDP and RDPClip.exe doesn't have any command line arguments so it can't be used programmatically. The ideal solution would be to VPN to the server and then any programmatic access can be done using native tools such as robocopy. To extend your answer you could recommend a VPN.
  • GuitarPicker
    GuitarPicker over 8 years
    I was trying to stay as close to the original request as possible, since I don't know the nature of the network. It could be that the server is on a firewalled DMZ network only available via RDP. My solution would be two parts - the RDP connection, and a script to run from the remote machine. Please note that \\tsclient is not a traditional server mapping, although it acts like one. It does not require network connectivity other than the RDP session through which it is tunneling. To clarify the answer a bit, tsclient is the literal name to use, not a placeholder.
  • Matthew Peters
    Matthew Peters over 8 years
    So I cannot map a network drive because the security is locked down. Is there are way to use some native windows process (like rdpclip.exe) to just programmatically do what I can do manually with the clicking (copy and paste). Hypothetically, I see no reason this cannot be accomplished. The user is sending a command somewhere and the OS is interpreting that to actually perform the transfer....
  • GuitarPicker
    GuitarPicker over 8 years
    @Matthew: The solution I gave does not involve network drives. It uses the same syntax as a network drive, but it is not one. It all tunnels through the remote desktop session. You will have to manually or otherwise establish a remote desktop session to the computer with drive sharing enabled, but once that is complete, you can connect to \\tsclient\c from the remote computer to get to the local computer's C drive. The link I included has screenshots of the whole process. Try it out. I use this technique frequently to move files to and from a remote machine that is on a secure network.
  • GuitarPicker
    GuitarPicker over 8 years
    Old versions of RDP used to have a command line option to run an application when you connected, but I think this was removed a while back. Too bad, because it would really come in handy for what you are trying to do.
  • Matthew Peters
    Matthew Peters over 8 years
    I may have called it the incorrect term but I cannot link my workstation's drives directly to the remote via the RDP client as described in the article you mentioned.
  • GuitarPicker
    GuitarPicker over 8 years
    Ouch. It's possible that feature is being blocked by the server or through Group Policy. If that's the case, you may have to beg for an exception. Other convoluted ways of doing this are through some sort of Auto Hotkeys magic, but I'll let someone else write that answer.
  • Matthew Peters
    Matthew Peters over 8 years
    Without digging too deep into clipboard RDC (which I will do anyway later), is there a native windows command to load something into the clipboard? If there is, I could essentially do just what the FOSS does but natively.
  • harrymc
    harrymc over 8 years
    I mentioned that project as an example. If you are into Win32 API, then start with the SetClipboardData function. C/C++ will be easier than Java for interfacing with Windows.
  • harrymc
    harrymc over 8 years
    You could also use the free utility nircmd.exe clipboard commands to load a file into and store from the clipboard.
  • Matthew Peters
    Matthew Peters over 8 years
    To complete your answer, you need to add that the user needs to also add a script on the remote side to retrieve the copied file. Additionally, these two scripts would have to be in sync or at least responsive to each other. Furthermore, I'm not really sure if this will work and cannot test it myself (security issues on my local machine). If you could test it and address my first two points, this would be the simplest solution by far...
  • Friesgaard
    Friesgaard about 6 years
    This is exactly what I was looking for! Thanks
  • vaibhavcool20
    vaibhavcool20 almost 6 years
    is there a way to copy from server without mapping the drive? can you provide some resources?
  • Michael Haephrati
    Michael Haephrati over 5 years
    Could you give an example? Let's say the server is 192.11.12.33