remote desktop session manager

by veroli at 2012-10-10 07:29:15

Hi all,
as part of my learning experience with powershell i thought i would give a little bit back.
Here is my first forms based powershell script.
It allows you to view a list of remote desktop sessions from a list of servers and kill the session if required.
The script has been hacked around from some existing code out there and is essentially a rewrite of an old kix script of mine.
You will need psterminalservices module installed and text file called servers.txt in the same location as the script with a machine hostname on each line.
Hope this helps someone and am sure it could be quite easily developed and enhanced with functionality and looks. Hope its ok to post here if not feel free to remove

Dan

# Depends on PSTerminalServices module
# Ini file is servers.txt which is parsed to provide a list of servers to manage.
# Keep servers.txt in the same location as main script.
# Place a server hostname on a new line in the file with a carriage return at the end.
# Further comments explain functionality of script
#
# ======================================================================================

# Import PSTerminalservices Module
import-module psterminalservices
$erroractionpreference = "SilentlyContinue"

# Set up the form and objects

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Remote Desktop Session Manager"
$objForm.Size = New-Object System.Drawing.Size(870,800)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True

# Buttins Initialise
$button1 = New-Object System.Windows.Forms.Button
$button2 = New-Object System.Windows.Forms.Button
$button3 = New-Object System.Windows.Forms.Button

# Labels
$label1 = New-Object Windows.Forms.Label
$label1.Location = New-Object Drawing.Point (50,25)
$label1.Size = New-Object Drawing.Point (100,15)
$label1.text = "Servers"
$objForm.controls.add($label1)

$label2 = New-Object Windows.Forms.Label
$label2.Location = New-Object Drawing.Point (240,25)
$label2.Size = New-Object Drawing.Point (100,15)
$label2.text = "Users"
$objForm.controls.add($label2)

$label3 = New-Object Windows.Forms.Label
$label3.Location = New-Object Drawing.Point (240,395)
$label3.Size = New-Object Drawing.Point (100,15)
$label3.text = "Session Details"
$objForm.controls.add($label3)

# Add the listbox containing the server names

$objServersListbox = New-Object System.Windows.Forms.Listbox
$objServersListbox.Location = New-Object System.Drawing.Size(50,40)
$objServersListbox.Size = New-Object System.Drawing.Size(150,640)
$objServersListBox.Sorted = $True
$objForm.Controls.Add($objServersListbox)


# Add the listbox to display the users

$objUsersListbox = New-Object System.Windows.Forms.Listbox
$objUsersListbox.Location = New-Object System.Drawing.Size(240,40)
$objUsersListbox.Size = New-Object System.Drawing.Size(150,320)
$objUsersListBox.Sorted = $True
$objForm.Controls.Add($objUsersListbox)


# Add a text box to the form to display the session details

$objOutputBox = New-Object System.Windows.Forms.TextBox
$objOutputBox.Location = New-Object System.Drawing.Size(240,410)
$objOutputBox.Size = New-Object System.Drawing.Size(460,260)
$objOutputBox.Multiline = $True
$objOutputBox.Font = New-Object System.Drawing.Font("Courier New", "8.5")
$objOutputBox.Wordwrap = $True
$objOutputBox.ReadOnly = $True
$objOutputBox.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
$objForm.Controls.Add($objOutputBox)


# Add Buttons to Forms

# Button 1

$button1.TabIndex = 1
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 95
$System_Drawing_Size.Height = 23
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True
$button1.Text = "Refresh session"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 720
$System_Drawing_Point.Y = 600
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($button1_OnClick)
$objForm.Controls.Add($button1)

# Button 2

$button2.TabIndex = 2
$button2.Name = "button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 95
$System_Drawing_Size.Height = 23
$button2.Size = $System_Drawing_Size
$button2.UseVisualStyleBackColor = $True
$button2.Text = "Kill Session"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 720
$System_Drawing_Point.Y = 645
$button2.Location = $System_Drawing_Point
$button2.DataBindings.DefaultDataSourceUpdateMode = 0
$button2.add_Click($button2_OnClick)
$objForm.Controls.Add($button2)

# Button 3

$button3.TabIndex = 3
$button3.Name = "button3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 85
$System_Drawing_Size.Height = 23
$button3.Size = $System_Drawing_Size
$button3.UseVisualStyleBackColor = $True
$button3.Text = "Refresh users"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 410
$System_Drawing_Point.Y = 335
$button3.Location = $System_Drawing_Point
$button3.DataBindings.DefaultDataSourceUpdateMode = 0
$button3.add_Click($button3_OnClick)
$objForm.Controls.Add($button3)


# get all servers from text file called

$items = get-content .\servers.txt


# populate the list box that displays the servers

foreach ($item in $items)
{

[void] $objServersListbox.Items.Add($item)
}


# Add the subroutine for retrieving users logged in to a sever

$objServersListbox.Add_Click(
{
$server_name = $objServersListbox.SelectedItem
$items = Invoke-Expression "get-tssession -comp $server_name"
$objUsersListbox.Items.Clear()

if ($items –ne $Null)
{
foreach ($item in $items)
{
[void] $objUsersListbox.Items.Add($item.Username)
}
}
})


# Add the subroutine for remote desktop sessions and output to textbox

$objusersListbox.Add_Click(
{
$servers1 = $objServersListbox.SelectedItem
$users1 = $objUsersListbox.SelectedItem
$objOutputBox.Text = Invoke-Expression "(get-tssession -comp $servers1 -username $users1 | Format-List | Out-String).Trim()"
})

# Methods for Buttons
# Button 1 refresh Session details

$button1.add_click(
{
$servers1 = $objServersListbox.SelectedItem
$users1 = $objUsersListbox.SelectedItem
$objOutputBox.Text = Invoke-Expression "(get-tssession -comp $servers1 -username $users1 | Format-List | Out-String).Trim()"
})


# Button Kill a Users Session

$button2.add_click(
{
$sessionid1 = invoke-expression "(get-tssession -comp $servers1 -username $users1 | select-object -expand sessionid)"
stop-tssession -comp $servers1 $sessionid1 -Force
})


# Button 3 Refresh users on a server

$button3.add_click(

{$server_name = $objServersListbox.SelectedItem
$items = Invoke-Expression "get-tssession -comp $server_name"
$objUsersListbox.Items.Clear()
if ($items –ne $Null)
{
foreach ($item in $items)
{
[void] $objUsersListbox.Items.Add($item.Username)
}
}
})


# Activate the form

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
by JeffH at 2012-10-10 18:36:24
You might also consider posting this at Poshcode.org. That is where many people go to look for PowerShell scripts.