Monday, September 3, 2007

Powershell script to create Remote Desktop connection files

I have several hundred servers I manage. I like to keep a folder on my "Quick Launch" bar called "RDP" with all the connection files to the servers contained within, broken down by environment.

After the first time of needing to change one setting, I decided to script the creation of these files.

Prerequisites:
  1. PowerShell 1.0 installed (which requires .NET Framework 2.0)
  2. Windows XP or Server 2003 (not tested on 2000)
What this script does:
  1. It creates subfolders based on predefined resolutions (800x600, 1024x768, and fullscreen). I added this because I sometimes control my work machine from home which has a lower resolution (and I rarely like working fullscreen)
  2. It creates subfolders organized as specified in a comma-delimited file to create the RDP files in, so I can have one for each environment we have (QA, Staging, Production, etc)
  3. It deletes existing RDP files within the folder structure before creating new ones. Note, however, I do not delete all RDP files. If you remove an entry from the CSV file, it will remain unless you delete the subfolders manually.
Script files
  • You can download everything mentioned in this article by clicking here: CreateRDP_7.9.23.rar
  • Note: the above archive contains an example shortcut hard-coded based on my computer. You'll want to edit it or create a new one.

Steps:
  1. Create a folder to contain the RDP files. We'll use "RDP" here
  2. Within "RDP" create a folder called "_script"
  3. Within "RDP\_script" create the following:
    1. A comma-delimited file called "rdplist.csv" with the following column headings: "ServerName", "IP", "Directory" (first row should read: "ServerName,IP,Directory" if using a text-editor). For each row, fill in the proper values for each server. "IP" can be either the IP address or FQDN. "Directory" refers to the subfolder of "RDP" that will be created for this server.
    2. A text file called "CreateRDP.ps1" with the following (take special care to preserve the back-tick-N "`n" as that is the PowerShell code for a new line, necessary for adding into the RDP file):

    3. $List = Import-CSV RDPLIST.CSV
      $resolutions = ("fullscreen","800x600","1024x768")
      ForEach($Entry in $List) {
      ForEach($resolution in $resolutions) {
      # Prepend the destination directory info for RDP files
      $Dir = "..\" + $resolution + "\" + $Entry.Directory

      # Create new folder
      New-Item -Path $Dir -ItemType Directory -Force

      # Build the file name
      $FileName = $Dir + "\" + $Entry.ServerName + ".RDP"

      # Remove the old file
      Remove-Item $FileName -Force

      # Begin building RDP file
      $temp = "`nfull address:s:" + $Entry.IP

      switch ($resolution) {

      "fullscreen" {
      $temp = $temp + "`nscreen mode id:i:2"
      }

      "800x600" {
      $temp = $temp + "`nscreen mode id:i:1"
      $temp = $temp + "`ndesktopwidth:i:800"
      $temp = $temp + "`ndesktopheight:i:600"
      }

      "1024x768" {
      $temp = $temp + "`nscreen mode id:i:1"
      $temp = $temp + "`ndesktopwidth:i:1024"
      $temp = $temp + "`ndesktopheight:i:768"
      }
      }
      $temp | out-file $FileName
      write-host $temp
      get-content template_bottom.txt >> $FileName
      }
      }

  4. Create a shortcut of the script and save it under "RDP" taking care that the "Start In" folder is still "RDP\_script". This is necessary if you want to just be able to click the file and have it create / update your RDP files. Otherwise, PowerShell will complain about not finding the script and I prefer to not hardcode paths within the script.
  5. Create a text file called "template_bottom.txt" and save it in "RDP\_script". This file, and example of which is below, should contain all of the RDP settings you want to use EXCEPT the actual server IP/FQDN and screen resolution. A good way to get these values is to create an RDP profile with all the settings you want and then save it here without the server IP/FQDN and screen resolution line (see script above for which to remove):


  6. audiomode:i:2
    authentication level:i:0
    autoreconnection enabled:i:1
    bitmapcachepersistenable:i:1
    compression:i:1
    disable cursor setting:i:0
    disable full window drag:i:1
    disable menu anims:i:1
    disable themes:i:0
    disable wallpaper:i:1
    displayconnectionbar:i:1
    keyboardhook:i:2
    redirectcomports:i:0
    redirectdrives:i:1
    redirectprinters:i:0
    redirectsmartcards:i:0
    session bpp:i:16

  7. Double-click the shortcut within "RDP" and see how it works!
Note, you may have to enable script execution. Powershell by default does not automatically execute scripts. Microsoft, being overly paranoid after .vbs files being used as trojan and virus entry points, set the default execution policy for Powershell scripts to "deny". To enable scripts, you'll need to either sign the scripts with a trusted certificate or, as I do, just enable script execution:
"PS c:\Set-ExecutionPolicy Unrestricted"

For those who asked how to save credentials in this script, EverydayNerd has made an improvement on my script which does just that. Check it out: http://everydaynerd.com/microsoft/save-password-in-rdp-file

4 comments:

Anonymous said...

Did you ever determine how to generate and save RDP password hashes?

Aaron Dodd said...

Unfortunately that is not possible as part of the .rdp file. The password hash entry was only used in the first version of remote desktop and is ignored now. Recent versions use the registry to store these.

Anonymous said...

Thats very odd because I'm running XP SP3 and have got a perfectly running default.rdp file with password hash. Users have fixed and shared profiles so I don't think it can be in the registry.

Aaron Dodd said...

I certainly could be wrong, I based that on some futile attempts to script the hash entry. If you copy that RDP file to another computer, does it still work?

I've long since stopped using Remote Desktop as its too much of a pain to administer, even with this script, for hundreds of connections (I now have to manage over 600 RDP connection profiles across three datacenters...).

We've instead purchased licenses for VisionApp Remote Desktop, which basically is a wrapper for RDP, VNC, ssh, and telnet (wrapping MS's RDP, and Putty for ssh/telnet) but with a central connection repository and the ability to have private or shared credentials.