Skip to Content

PowerShell-Windows.Forms RDP Launcher

In a given environment there can be many short term solutions that over time grow into long term headaches. We have a situation where we need to provide access to RDP sessions through Citrix. The original solution by previous admins was to create a custom RDP icon configured to the server in question and publish it in Citrix. This resulted in a rather 'busy' screen in Citrix and intermittant update issue.

A co-worker asked about PowerShell doing this and I said something off handed about Windows Forms to generate a menu selection should be do-able. He found a nice Windows.Forms example and then did a hard coded elseif menu (which seemed as bad as all the links to me). I came up with an improvement where we pulled the server/ip list from a file and used Select and Eric Woodford added a nicer $choice option with the added bonus of turning the line for launching RDP into a variable. This is good because we have a similar issue with putty links. This should wipe out 80 some odd custom published Citirx apps.

#############################################################################
#        Name:  choiceRDP.ps1
#        Date:  1/15/2011
# Description:  This script allows populates a list from a file with a launcher for RDP sessions
#      Source:  Windows.Forms reference ww w.powershell.nu/2009/01/21/dropdown-menu-using-windowsforms/
#############################################################################
 
## Get servers from a list and store in the array this allows for easier managability versus editing the script
## File format for 'choiceRDPservers.csv'
## Header Row with first line:  ServerName,IP
## Data on next line as      :  server01,192.168.1.2
##                           :  server05,192.168.16.25
 
$Servers = import-csv "C:\Scripts\choiceRDP\choiceRDPservers.csv"
$Servers = $servers | sort servername
 
## This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
 
 $Choice = $DropDown.SelectedItem.ToString()
 $findIP = $servers |?{$_.servername -match $choice}
 $exp = "mstsc.exe /v:"+$findip.ip+" /w:1024 /h:768 /console"
 
 write-host $exp
 #$Form.Close()
 
 Invoke-Expression $exp 
 #Write-Host $Choice 
}
 
 
## This loads the Classes into the Global Assembly Cache 
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
 
#This piece of code creates the body of the form and allows you to label it
$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 150
$Form.Text = ”Server RDP Launcher”
 
## This piece of code creates the DropDown List
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
 
foreach ($Item in $servers) {
 $DropDown.Items.Add($Item.servername)
}
 
$Form.Controls.Add($DropDown)
 
 
$Form.Controls.Add($DropDown)
 
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,20)
$DropDownLabel.Text = "Server List"
$Form.Controls.Add($DropDownLabel)
 
 
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(120,20)
$Button.Text = "Select This Server"
 
$Button.Add_Click({return-DropDown})
$form.Controls.Add($Button)
 
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

Commenting on this Blog entry is closed.