Search code examples
powershellrdp

Create multiple RDPs on network using Powershell


I have a seemingly simple task to accomplish - to create one (or more) Remote Desktop connection files on multiple machines on the network. Our employees connect to one of several servers on a daily basis and going in to setup each .RDP file every time we hire a new employee can become time consuming. So I am trying to streamline this operation by having powershell go and create the .RDP filess for me on each machine. (if it's not already there)

I used this person's tutorial and code to get it working on my machine. I run the code in powershell and it creates the RDP connection files I want on my machine.

However, now I am trying to figure out how to have it so i can do this from one machine (preferably the main server) and set up each .RDP file with the correct username on that individual's machine.


Solution

  • Powershell's architect wrote a blog post in reply to the tutorial you linked. The script below follows some of his suggestions, and makes several assumptions about your needs.

    Let's say users Joe, Sally, and Frank will make regular rdp connections to 3 servers, TS1, TS2, and TS3. Joe's workstation is called CLTCOMP1, Sally's is CLTCOMP2, and Frank's is CLTCOMP3. The script assumes the users login to the servers and the workstations with their domain accounts, and it creates output files called targetServer-username.rdp and then copies them to the user's Documents folder on each workstation. The Copy assumes the client workstations run Vista or higher and that you can see and write to that destination as a UNC path. It would be trivial to adjust the $destPath for XP clients. From your tutorial, I omitted the logic about screen resolution and subfolders, for the sake of overall simplicity. If needed, that logic could probably be added back in without too much trouble.

    The script imports from rdpList.csv with content like this:

    USERNAME,COMP
    joe,CLTCOMP1
    sally,CLTCOMP2
    frank,CLTCOMP3
    

    Here's the script:

    $targetServers = "TS1","TS2","TS3"
    $resWidth = 1024
    $resHgt = 768
    $domain = "DOMAIN"
    #####################
    
    $hereString = @"
    
    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:1
    disable wallpaper:i:1
    displayconnectionbar:i:1
    keyboardhook:i:2
    redirectclipboard:i:1
    redirectcomports:i:0
    redirectdrives:i:0
    redirectprinters:i:0
    redirectsmartcards:i:0
    session bpp:i:16
    prompt for credentials:i:0
    promptcredentialonce:i:1
    "@
    
    $list = import-csv rdpList.csv
    forEach ($ts in $targetServers) {
     forEach ($item in $list) {
      $out = @()
      $out += "full address:s:" + $ts
      $out += "screen mode id:i:1"
      $out += "desktopwidth:i:" + $resWidth
      $out += "desktopheight:i:" + $resHgt
      $out += "username:s:" + $domain + "\" + $item.username
      $out += $hereString
      $outFileName = ".\" + $ts + "-" + $item.username + ".rdp"
      $out | out-file $outFileName
      $destPath = "\\" + $item.comp + "\c$\users\" + $item.username + "\documents\"
      copy-item $outFileName $destPath  
     } #closes foreach item
    } #closes foreach ts