RDP by selecting list of systems

Just another Tech site

RDP by selecting list of systems

I was looking for a way to select from a list of systems to RDP into and came up with a combo box inside of a form.  Simple form display

 

 

 

 

 

 

 



Add-Type -AssemblyName System.Windows.Forms

# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "RDP Connection"
$form.Size = New-Object System.Drawing.Size(600,250)
$form.StartPosition = "CenterScreen"

# Create the ComboBox
$comboBox = New-Object System.Windows.Forms.ComboBox
$comboBox.Location = New-Object System.Drawing.Point(50,50)
$comboBox.Size = New-Object System.Drawing.Size(200,20)
$comboBox.font = New-Object System.Drawing.Font("Lucida Console",14,[System.Drawing.FontStyle]::Regular)

# Add server list to ComboBox
$servers = @("BAT1", "BAT2", "BAT3") # Replace with your server list
$comboBox.Items.AddRange($servers)

# Create the button
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(100,100)
$button.Size = New-Object System.Drawing.Size(100,30)
$button.Text = "Connect"

# Add event to button
$button.Add_Click({
    $selectedServer = $comboBox.SelectedItem
    if ($selectedServer) {
        mstsc /v:$selectedServer
    } else {
        [System.Windows.Forms.MessageBox]::Show("Please select a server.")
    }
})

# Add controls to the form
$form.Controls.Add($comboBox)
$form.Controls.Add($button)

# Show the form
$form.Add_Shown({$form.Activate()})
[void]$form.ShowDialog()

Added update to use a text file with a list of servers, and just update the list to get the servers that you want to RDP into remotely.

Add-Type -AssemblyName System.Windows.Forms


# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "RDP Connection"
$form.Size = New-Object System.Drawing.Size(600,250)
$form.StartPosition = "CenterScreen"

# Create the ComboBox
$comboBox = New-Object System.Windows.Forms.ComboBox
$comboBox.Location = New-Object System.Drawing.Point(50,50)
$comboBox.Size = New-Object System.Drawing.Size(200,20)
$comboBox.font = New-Object System.Drawing.Font("Lucida Console",14,[System.Drawing.FontStyle]::Regular)



# Read servers from file location
$serverFile = "D:\Lists\servers.txt"
if (Test-Path $serverFile) {
    $servers = Get-Content $serverFile
    foreach ($server in $servers) {
        $combobox.Items.Add($server)
    }
}

# Add server list to ComboBox
#$servers = @("BAT1", "BAT2", "BAT3") # Replace with your server list
#$comboBox.Items.AddRange($servers)

# Create the button
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(100,100)
$button.Size = New-Object System.Drawing.Size(100,30)
$button.Text = "Connect"

# Add event to button
$button.Add_Click({
    $selectedServer = $comboBox.SelectedItem
    if ($selectedServer) {
        mstsc /v:$selectedServer
    } else {
        [System.Windows.Forms.MessageBox]::Show("Please select a server.")
    }
})

# Add controls to the form
$form.Controls.Add($comboBox)
$form.Controls.Add($button)

# Show the form
$form.Add_Shown({$form.Activate()})
[void]$form.ShowDialog()