Advice May

A Script to Create a RDP File

In my business I build servers for clients and have automated many of the procedure in that process. One of the final steps in the build process is to create RDP (remote desktop protocol) files to give the clients so they can connect to the servers. Until last week that process was manual and mind numbing. I always had in the back of my mind “How can I script that part???” Last week I had some time and decided to make that happen. I did some looking on the web and found Shawn Melton’s blog “look what I did with PowerShell”. This was almost exactly what I wanted so I downloaded the code and started to make any changes that would fit my environment.

My script has 4 parameters. I made 3 mandatory and 1 optional but you will see that if you are creating several RDP files it will require all the parameters.
 $server – this is for the server you will be connecting to
$MyDefaultRDP – this references the default RDP file. This needs to be saved to the folder where you save the script. I opened an rdp connection and made all the configurations I wanted and then saved it to the folder where the script is saved with the file name default.rdp. You then need to open the RDP file with notepad and remove the line – “full address:s:” this line will get generated in the script
 $myAccount – this is the username that will be connecting to the RDP session
$file – this is the name for the file that the script generates. Shawn used the server name for his but I had to add this new parameter as my server names violated file name conventions.

Examples: this assumes you save your script as newRDP.ps1 have started powershell and changed to the directory where the file is located. If the script fails it could be due to script execution. If that is the issue you can type in “set-executionpolicy unrestricted”. This does open up execution of PowerShell scripts and could be considered unsafe. Check out TechNet for more information on this cmdlet.

 

 .\newrdp.ps1 computer42 .\default.rdp user01 rdpfile – this will create and RDP file named rdpfile.rdp the computer set to computer 42 and the user set to user01

 


I went 1 step farther I needed to create 50 RDP files so I created a CSV file with 3 columns:  user, file and server. Populated these with the proper information  and saved the file as users.csv in the same directory as the newrdp.ps1 file. I then created and new ps1 file with the following code in it and saved it as rdps.ps1

 

 $users = Import-Csv .\users.csv
Foreach($usr in $users){
.\NewRDPsv3.ps1 -server $usr.Server -mydefaultrdp .\Default.rdp -myaccount $usr.User -file $usr.file
}


Code for the new RDP script
param(
 [Parameter(Position=0,Mandatory=$True)]
 [string]$server,
 [Parameter(Position=1,Mandatory=$True)]
 [string]$MyDefaultRDP,
 [Parameter(Position=2,Mandatory=$False)]
 [string]$myaccount,
 [Parameter(Position=3,Mandatory=$True)]
 [string]$file
 )

ForEach($entry in $file)
{
 #build the file
 #assumes current directory
 $filename = ".\" + $entry + ".rdp"
 
 #add hostname and username /if provided/ to rdp file
 
 # Add hostname in RDP file
 $temp = "`nfull address:s:" + $server
 
 #test for username, if empty it will not be added
 if ($myaccount) {
  $temp = $temp + "`r`nusername:s:" + $myaccount
 }
 #check to see if file exist
 if (Test-Path $filename) {
  Remove-Item $filename -force
  Write-Host "Found $filename existed, so I deleted it for you" }
 else {
  $temp | Out-File $filename
 }
 Get-Content $MyDefaultRDP | Out-File $filename -Append
 Write-Host "$filename created"
}

<< Back to Advice