2018-03-29 12:33:28 -05:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
This script was started due to the tendency of programs to crash when managing simple RDP connections
|
|
|
|
And not being able to store creds. This will auto-type them for you.
|
|
|
|
.PARAMETER CSVPath
|
|
|
|
Path to the CSV file with the following column headers:
|
|
|
|
ServerName,IPAddress,Port,LogonDomain,LogonUserID,ScreenSize,Console,RestrictedAdmin,Width,Height,Span,MultiMon,Prompt,Gateway
|
|
|
|
.NOTES
|
2018-03-29 13:13:44 -05:00
|
|
|
Name: Manage-RDPConnection.ps1
|
2018-03-29 12:33:28 -05:00
|
|
|
Author: Dan Hamik
|
|
|
|
Contact: Email - danhamik@gmail.com
|
|
|
|
DateCreated: 2018-03-23
|
|
|
|
Version: 1.0
|
|
|
|
.EXAMPLE
|
|
|
|
TO DO
|
|
|
|
#>
|
|
|
|
[cmdletbinding(SupportsShouldProcess, ConfirmImpact = 'high')]
|
|
|
|
param(
|
|
|
|
[parameter()][string]$CSVPath="$env:USERPROFILE\Documents\RDPConnections.csv",
|
|
|
|
[parameter()][switch]$Edit
|
|
|
|
)
|
|
|
|
Add-Type @"
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
public class WinAp {
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
|
|
}
|
|
|
|
|
|
|
|
"@ -ErrorAction SilentlyContinue
|
|
|
|
$headers = @("ServerName", "GroupName", "IPAddress", "Port", "LogonDomain", "LogonUserID", "ScreenSize", "Console", "Width", "Height", "Span", "MultiMon", "Gateway")
|
2018-03-29 13:13:44 -05:00
|
|
|
$creds = @{}
|
2018-03-29 12:33:28 -05:00
|
|
|
function Global:connect-server(){
|
|
|
|
param(
|
|
|
|
[parameter(Mandatory)][int]$ServerInfo
|
|
|
|
)
|
|
|
|
$myserver = $Global:RDPList[$ServerInfo]
|
2018-03-29 13:13:44 -05:00
|
|
|
$credinfo = "$($myserver.LogonDomain)\$($myserver.logonuserid)"
|
|
|
|
if ($creds["$credinfo"]) {
|
|
|
|
$cred = $creds["$credinfo"]
|
|
|
|
} else {
|
|
|
|
$creds["$credinfo"] = Get-Credential
|
|
|
|
$cred = $creds["$credinfo"]
|
2018-03-29 12:33:28 -05:00
|
|
|
}
|
|
|
|
$process = start-process mstsc.exe -ArgumentList "/v:$($myserver.ServerName) /public" -PassThru
|
|
|
|
[void][WinAP]::SetForegroundWindow($process.MainWindowHandle)
|
|
|
|
[void][Winap]::ShowWindow($process.MainWindowHandle, 5)
|
|
|
|
[void][Winap]::ShowWindow($process.MainWindowHandle, 5)
|
|
|
|
start-sleep 2
|
|
|
|
#write-output $cred.getnetworkcredential().password
|
2018-03-29 13:13:44 -05:00
|
|
|
[System.Windows.Forms.SendKeys]::SendWait("$($cred.username){TAB}")
|
2018-03-29 12:33:28 -05:00
|
|
|
start-sleep .5
|
2018-03-29 13:13:44 -05:00
|
|
|
[System.Windows.Forms.SendKeys]::SendWait("$($cred.getnetworkcredential().password){enter}")
|
2018-03-29 12:33:28 -05:00
|
|
|
}
|
|
|
|
function Show-Form() {
|
|
|
|
Add-Type -AssemblyName System.Windows.Forms
|
|
|
|
$form = New-Object Windows.Forms.Form
|
2018-03-29 13:13:44 -05:00
|
|
|
#$form.Size = New-Object Drawing.Size @(200, 100)
|
2018-03-29 12:33:28 -05:00
|
|
|
$form.StartPosition = "CenterScreen"
|
|
|
|
$form.AutoSize = $true
|
2018-03-29 13:13:44 -05:00
|
|
|
$form.AutoScroll=$false
|
|
|
|
$form.VerticalScroll.Visible=$false
|
2018-03-29 12:33:28 -05:00
|
|
|
$maintable = New-Object System.Windows.Forms.TableLayoutPanel
|
|
|
|
$maintable.Dock = [System.Windows.Forms.DockStyle]::Top
|
|
|
|
$maintable.autosize = $true
|
|
|
|
$maintable.CellBorderStyle = "Inset"
|
2018-03-29 13:13:44 -05:00
|
|
|
$maintable.VerticalScroll.Visible=$true
|
|
|
|
$maintable.AutoScroll=$true
|
2018-03-29 12:33:28 -05:00
|
|
|
$form.controls.add($maintable)
|
|
|
|
#$maintable.VerticalScroll.Visible=$true
|
|
|
|
$gb = @{}
|
2018-03-29 13:13:44 -05:00
|
|
|
$gbindex=0
|
2018-03-29 12:33:28 -05:00
|
|
|
$gt = @{}
|
|
|
|
$index=0
|
|
|
|
foreach ($groupname in ($Global:RDPList|Select-Object -property GroupName -Unique).groupname) {
|
|
|
|
if ($groupname -eq "") {
|
|
|
|
$groupname = "NONE"
|
|
|
|
}
|
|
|
|
$mygb = new-object Windows.Forms.GroupBox
|
|
|
|
$mygt = New-Object System.Windows.Forms.TableLayoutPanel
|
|
|
|
$mygb.dock = 'left'
|
|
|
|
$mygt.dock='fill'
|
|
|
|
$mygb.autosize = $true
|
|
|
|
$mygt.autosize = $true
|
|
|
|
$mygt.CellBorderStyle = "Inset"
|
|
|
|
$mygb.text = $groupname
|
|
|
|
$mygb.Controls.add($mygt)
|
|
|
|
$gb.add($groupname, $mygb)
|
|
|
|
$gt.add($groupname, $mygt)
|
2018-03-29 13:13:44 -05:00
|
|
|
$maintable.controls.add($mygb, $gbindex, $index)
|
|
|
|
|
|
|
|
if ($gbindex -eq 1) {$gbindex = 0; $index++}else {$gbindex++}
|
2018-03-29 12:33:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$GroupColumn = @{}
|
|
|
|
$GroupRow = @{}
|
|
|
|
for ($i = 0; $i -le ($Global:RDPList.count - 1) ; $i++) {
|
|
|
|
$servername = $Global:RDPList[$i].ServerName
|
|
|
|
write-verbose "Working on $servername"
|
|
|
|
$btn = New-Object System.Windows.Forms.Button
|
|
|
|
$btn.Text = $servername
|
|
|
|
$btn.AutoSize = $true
|
|
|
|
$btn.anchor='left','right'
|
|
|
|
$btn.add_click( { & connect-server -ServerInfo $i }.GetNewClosure() )
|
|
|
|
$groupname = $Global:RDPList[$i].GroupName
|
|
|
|
write-verbose $groupname
|
|
|
|
if(([string]::IsNullOrEmpty($GroupColumn["$groupname"]))){
|
|
|
|
$GroupColumn["$groupname"]=0
|
|
|
|
}
|
|
|
|
if(([string]::IsNullOrEmpty($GroupRow["$groupname"]))){
|
|
|
|
$GroupRow["$groupname"]=0
|
|
|
|
}
|
|
|
|
write-verbose "$($GroupRow["$groupname"]) $($GroupColumn["$groupname"])"
|
|
|
|
write-verbose $btn.size
|
|
|
|
$gt["$groupname"].Controls.Add($btn,$GroupColumn["$groupname"],$GroupRow["$groupname"])
|
2018-03-29 13:13:44 -05:00
|
|
|
if ($GroupColumn["$groupname"] -eq 2) {$GroupColumn["$groupname"] = 0; $GroupRow["$groupname"]++} else { $GroupColumn["$groupname"]++}
|
2018-03-29 12:33:28 -05:00
|
|
|
}
|
|
|
|
$form.width = $maintable.Width
|
|
|
|
$drc = $form.ShowDialog()
|
|
|
|
}
|
|
|
|
function add-server() {
|
|
|
|
#$newRow = new-object psobject -property @{ ServerName = (read-host "SERVERNAME") ; LogonDomain = (read-host "DOMAINNAME"); GroupName = (Read-Host "GroupName")}
|
|
|
|
$newrow =""
|
|
|
|
foreach ($header in $headers){
|
|
|
|
$newrow = $newrow + "`"$(read-host "$($header.toupper())")`","
|
|
|
|
}
|
|
|
|
add-content -path $csvpath -value $newrow
|
|
|
|
update-rdplist
|
|
|
|
$Global:RDPList | sort-object -property ServerName |Export-Csv -Path $CSVPATH -NoTypeInformation
|
|
|
|
|
|
|
|
#$Global:RDPList | sort-object -property ServerName |Export-Csv -Path $CSVPATH -NoTypeInformation
|
|
|
|
#$Global:RDPList = @($global:rdplist | sort-object -property ServerName)
|
|
|
|
}
|
|
|
|
function update-rdplist(){
|
|
|
|
$Global:RDPList = @(get-content -path $Global:CSVPath | select-object -skip 1 | convertfrom-csv -header $headers | sort-object -property ServerName)
|
|
|
|
#$global:RDPList = @($global:RDPList | sort-object -property ServerName)
|
|
|
|
}
|
|
|
|
function show-servers(){
|
|
|
|
foreach($index in 0 .. ($Global:RDPList.count-1)){
|
|
|
|
write-output "$($index +1) - $($Global:RDPList[$index].ServerName)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove-server() {
|
|
|
|
show-servers
|
|
|
|
$toremove = read-host "Which server would you like to remove?"
|
|
|
|
$newcsv = @()
|
|
|
|
foreach ($index in 0 .. ($Global:RDPList.count - 1)) {
|
|
|
|
if ($index -ne ($toremove - 1)) {
|
|
|
|
$newcsv += $Global:RDPList[$index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($newcsv.count -gt 0) {
|
|
|
|
$newcsv| sort-object -property ServerName |Export-Csv -Path $CSVPATH -NoTypeInformation
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set-content -path $CSVPath -Value "`"ServerName`",`"GroupName`",`"IPAddress`",`"Port`",`"LogonDomain`",`"LogonUserID`",`"ScreenSize`",`"Console`",`"Width`",`"Height`",`"Span`",`"MultiMon`",`"Gateway`""
|
|
|
|
}
|
|
|
|
|
|
|
|
update-rdplist
|
|
|
|
}
|
|
|
|
function show-menu() {
|
|
|
|
param(
|
|
|
|
[string]$Title = "-=-=-=-=-=-=-=-Main Menu-=-=-=-=-=-=-=-"
|
|
|
|
)
|
|
|
|
$menu = @"
|
|
|
|
$title
|
|
|
|
1: Show Server List
|
|
|
|
2: Add Server to List
|
|
|
|
3: Remove Server from List
|
|
|
|
Q: Quit
|
|
|
|
"@
|
|
|
|
|
|
|
|
write-output $menu
|
|
|
|
}
|
|
|
|
$Global:CSVPath = $CSVPath
|
|
|
|
if (-not(test-path $CSVPath)) {
|
2018-03-29 13:13:44 -05:00
|
|
|
$edit=$true
|
2018-03-29 12:33:28 -05:00
|
|
|
new-item $CSVPath
|
|
|
|
set-content -path $CSVPath -Value "`"ServerName`",`"GroupName`",`"IPAddress`",`"Port`",`"LogonDomain`",`"LogonUserID`",`"ScreenSize`",`"Console`",`"Width`",`"Height`",`"Span`",`"MultiMon`",`"Gateway`""
|
|
|
|
} elseif ( (get-content $CSVPath) -eq "") {
|
2018-03-29 13:13:44 -05:00
|
|
|
$edit=$true
|
2018-03-29 12:33:28 -05:00
|
|
|
set-content -path $CSVPath -Value "`"ServerName`",`"GroupName`",`"IPAddress`",`"Port`",`"LogonDomain`",`"LogonUserID`",`"ScreenSize`",`"Console`",`"Width`",`"Height`",`"Span`",`"MultiMon`",`"Gateway`""
|
|
|
|
}
|
|
|
|
|
|
|
|
update-rdplist
|
|
|
|
if ($edit) {
|
|
|
|
if ($Global:RDPList.count -eq 0) {
|
|
|
|
$addserver = read-host ("No servers found, do you want to add a server? Y/N")
|
|
|
|
if ($addserver -eq "Y") {
|
|
|
|
add-server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
do {
|
|
|
|
Show-Menu
|
|
|
|
$input = Read-Host "Please make a selection"
|
|
|
|
switch ($input) {
|
|
|
|
'1' {
|
|
|
|
cls
|
|
|
|
show-servers
|
|
|
|
} '2' {
|
|
|
|
cls
|
|
|
|
add-server
|
|
|
|
} '3' {
|
|
|
|
cls
|
|
|
|
remove-server
|
|
|
|
} 'q' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
until ($input -eq 'q')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Show-Form
|
|
|
|
}
|