-
Notifications
You must be signed in to change notification settings - Fork 10
/
RDSSessionMgt.psm1
285 lines (259 loc) · 10.2 KB
/
RDSSessionMgt.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#requires -version 5
<#
.SYNOPSIS
This is the "client" module to go with "client" script. This module contains
class definitions specific to RAS.
.DESCRIPTION
This module is imported by client.ps1, where all the events are initialized.
.INPUTS
None
.OUTPUTS
class definitions
RASMessageUI
RASSessionAppUI
RDSSessionMgt
!IMPORTANT - Classes can only be imported with the "using module" syntax. Import-Module does not import classes.
.NOTES
Version: 1.0
Purpose: Showcase powershell capabilities with RASAdmin
.EXAMPLE
using module ./client.psm1
#>
using assembly presentationFramework
using assembly windowsbase
using module '../PSUI/PSWindow/PSWindow.psm1'
using namespace 'RASAdmin'
using namespace 'RASAdminEngine.Core.OutputModels'
using namespace System.Security
using namespace System.Collections.ObjectModel
using namespace System.Windows
using namespace System.Windows.Controls
using namespace System.Collections.Generic
#################################################################
# Send message UI
#################################################################
class RASMessageUI : PSWindow {
RASMessageUI() :base("./resources/MessageWindow.xaml") {}
}
#################################################################
# Session applications UI
#################################################################
class RASSessionAppUI : PSWindow {
[ObservableCollection[ServerAppInfo]] $Applications
RASSessionAppUI([string] $path) :base($path) {}
[void] SetAppsDataGrid([List[ServerAppInfo]] $apps) {
[ObservableCollection[ServerAppInfo]]$oc = [ObservableCollection[ServerAppInfo]]::new()
foreach ($app in $apps) {
$oc.Add($app)
}
$this.Applications = $oc
$this.dtApplications.ItemsSource = $this.Applications
}
}
#################################################################
# Main application window
#################################################################
class RDSSessionMgt : PSWindow {
##################################################
# public proerties
##################################################
[ObservableCollection[RDPSession]] $Sessions
##################################################
##################################################
# ctor
##################################################
RDSSessionMgt([string] $path) :base($path) {
if ([string]::IsNullOrEmpty($this.txtServer.Text)) {
$s = [System.Net.Dns]::GetHostName()
$this.txtServer.Text = $s
}
}
##################################################
# Get the scripblock text and assigns it to the the txtCmd.
# Values contained in the scriptblock will not be evaluated,
# i.e. some variable $a will be shown as '$a' and not the
# value it holds.
[void] UpdateCommandText([scriptblock]$cmd) {
$this.txtCmd.Text = $cmd.ToString()
}
# Refreshes the dtSessions Items with RDSSession
[void] Refresh() {
$tmp = $this.GetRDSSessions()
if ($tmp -eq $null) {
Write-Host "Failed to refresh" -ForegroundColor red
return
}
$this.SetSessionsDataGrid($tmp)
$this.dtSessions.ItemsSource = $tmp
Write-Host "Refreshed" -ForegroundColor Green
}
# Sends a message to the selected session.
[void] OnBtnSendMessageClick() {
try {
if (([DataGrid]$this.dtSessions).SelectedItems.Count -eq 0) {
Write-Warning "Please select a session"
return
}
$msgWindow = [RASMessageUI]::new()
$msg = ""
$rootwnd = $this
$msgWindow.btnSend.Add_Click({
$msg = $msgWindow.txtMessage.Text
if ([String]::IsNullOrEmpty($msg)) {
Write-Warning "Please input a message text"
return
}
[RDPSession]$item = ([DataGrid]$rootwnd.dtSessions).SelectedItem
$res = {Invoke-RASRDSSessionCmd -InputObject $item -Command SendMsg -MsgTitle "Message from $(whoami)" -Message $msg}
$res.Invoke()
$rootwnd.UpdateCommandText($res)
$msgWindow.Close()
})
$msgWindow.Show()
} catch [Exception] {
Write-Host "Failed to send message" -ForegroundColor red
Write-Host $_.Exception.Message -ForegroundColor red
}
}
# Creates a New-RASSession. Default server value is 127.0.0.1
[void] OnBtnLoginClick() {
try {
$u = $this.txtUsername.Text
$p = ConvertTo-SecureString -AsPlainText $this.txtPassword.Password -Force
$s = $this.txtServer.Text
if ([string]::IsNullOrEmpty($s)) {
$s = [System.Net.Dns]::GetHostName()
$this.txtServer.Text = $s
}
$cmd = {New-RASSession -Username $u -Password $p -Server $s}
$cmd.Invoke()
$this.UpdateCommandText($cmd)
[MessageBox]::Show("Session created for $($u).")
Write-Host "Created session for $u" -ForegroundColor Green
$this.Refresh()
}
catch [Exception] {
Write-Host "Could not login." -ForegroundColor red
Write-Host $_.Exception.Message -ForegroundColor red
[MessageBox]::Show("Could not login. $($_.Exception.Message)")
}
}
[void] OnBtnRefreshClick () {
$this.Refresh()
}
# Disconnects from the selected session.
[void] OnBtnDisconnectSessionClick() {
try {
[RDPSession]$item = ([DataGrid]$this.dtSessions).SelectedItem
if(!$item) {
Write-Warning "Please select a session"
return
}
$cmd = {Invoke-RASRDSSessionCmd -InputObject $item -Command Disconnect}
$cmd.Invoke()
$this.UpdateCommandText($cmd)
}
catch [Exception] {
Write-Host "Failed to disconnect session" -ForegroundColor red
Write-Host $_.Exception.Message -ForegroundColor red
}
}
# Log-out a selected session
[void] OnBtnLogoffSessionClick() {
try {
[RDPSession]$item = ([DataGrid]$this.dtSessions).SelectedItem
if(!$item) {
Write-Warning "Please select a session"
return
}
$cmd = {Invoke-RASRDSSessionCmd -InputObject $item -Command Logoff}
$cmd.Invoke()
$this.UpdateCommandText($cmd)
Write-Host "Removed $($item.User)" -ForegroundColor Green
$this.Sessions.RemoveAt(([DataGrid]$this.dtSessions).SelectedIndex)
} catch [Exception] {
Write-Host "Failed to Logoff session" -ForegroundColor red
Write-Error $_.Exception.Message
}
}
# Retrievies a list of application running on the selected session.
[void] OnBtnGetAppsClick() {
try {
[RDPSession]$item = ([DataGrid]$this.dtSessions).SelectedItem
if(!$item) {
Write-Warning "Please select a session"
return
}
$appsWindow = [RASSessionAppUI]::new("./resources/SessionApps.xaml")
$cmd = {Get-RASRDSStatus -StatusLevel Level3}
$this.UpdateCommandText($cmd)
$tmpStat = $cmd.Invoke()
foreach($s in $tmpStat.Sessions) {
if ($s.SessionID -eq $item.SessionID) {
$item = $s
}
}
$rootwnd = $this # inside the script block '$this' would refer to the scriptblock instance.
$appsWindow.SetAppsDataGrid($item.Applications)
$appsWindow.Form.Title = "Applications [$($item.SessionID) | $($item.User)]"
$appsWindow.btnKillProcess.Add_Click({
[ServerAppInfo]$app = ([DataGrid]$appsWindow.dtApplications).SelectedItem
if (!$app) {
Write-Warning "Please select an application"
return
}
$cmd = {Invoke-RASRDSProcessCmd -InputObject $app -Command Kill}
$cmd.Invoke()
$rootwnd.UpdateCommandText($cmd)
$appsWindow.Applications.RemoveAt(([DataGrid]$appsWindow.dtApplications).SelectedIndex)
$rootwnd.Refresh()
})
$appsWindow.btnRefresh.Add_Click({
$cmd = {Get-RASRDSStatus -StatusLevel Level3}
Write-Host "refresh apps"
$tmpStat = $cmd.Invoke()
foreach($s in $tmpStat.Sessions) {
if ($s.SessionID -eq $item.SessionID) {
$item = $s
Write-Host "item: "
Write-Host $item
}
}
$appsWindow.SetAppsDataGrid($item.Applications)
})
$appsWindow.Show()
} catch [Exception] {
Write-Host "Failed to get a list of applications" -ForegroundColor red
Write-Host $_.Exception.Message -ForegroundColor red
}
}
# Gets a list of RDSSessions.
[ObservableCollection[RDPSession]] GetRDSSessions() {
try {
$cmd = {Get-RASRDSStatus -StatusLevel Level2}
$tmpSessions = $cmd.Invoke()
$this.UpdateCommandText($cmd)
[ObservableCollection[RDPSession]]$oc = [ObservableCollection[RDPSession]]::new()
foreach ($st in $tmpSessions) {
foreach ($session in $st.Sessions) {
$oc.Add($session)
}
}
return $oc
} catch [Exception] {
Write-Host "Failed to get a list of RDSSessions" -ForegroundColor red
Write-Host $_.Exception.Message -ForegroundColor red
return $Null
}
}
[void] SetSessionsDataGrid([ObservableCollection[RDPSession]] $sessions) {
if ($sessions -eq $null) {
return
}
if ($sessions.Count -eq 0) {
return
}
$this.Sessions = $sessions
$this.dtSessions.ItemsSource = $this.Sessions
}
}