/
Control-MultiLauncher.ps1
244 lines (203 loc) · 10 KB
/
Control-MultiLauncher.ps1
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
#region Add Types
Add-Type -AssemblyName PresentationCore, PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
import-module $script:filestore\scripts\ControlFunctions.psm1
#endregion
#region Environment Tests
if((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain -eq $false){
[System.Windows.Forms.MessageBox]::Show("This machine is not joined to a domain")
set-location logs
Write-Log -Message "This machine is not joined to a domain" -Level Error -Path "event.log"
exit
}
#endregion
$Global:syncHash = [hashtable]::Synchronized(@{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Name = "GUI"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$psCmd = [PowerShell]::Create().AddScript({
[xml]$xaml = Get-Content xaml\multilauncher.xaml
$AttributesToRemove = @(
'x:Class',
'mc:Ignorable'
)
foreach ($Attrib in $AttributesToRemove) {
if ( $xaml.Window.GetAttribute($Attrib) ) {
$xaml.Window.RemoveAttribute($Attrib)
}
}
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
[xml]$XAML = $xaml
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object{
#Find all of the form types and add them as members to the synchash
$syncHash.Add($_.Name,$syncHash.Window.FindName($_.Name) )
}
$Script:JobCleanup = [hashtable]::Synchronized(@{})
$global:jobs = [system.collections.arraylist]::Synchronized((New-Object System.Collections.ArrayList))
#region Background runspace to clean up jobs
$jobCleanup.Flag = $True
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Name = "CleanUp"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("jobCleanup",$jobCleanup)
$newRunspace.SessionStateProxy.SetVariable("jobs",$global:jobs)
$jobCleanup.PowerShell = [PowerShell]::Create().AddScript({
#Routine to handle completed runspaces
Do {
Foreach($runspace in $jobs) {
If ($runspace.Runspace.isCompleted) {
[void]$runspace.powershell.EndInvoke($runspace.Runspace)
$runspace.powershell.dispose()
$runspace.Runspace = $null
$runspace.powershell = $null
}
}
#Clean out unused runspace jobs
$temphash = $jobs.clone()
$temphash | Where-Object {
$_.runspace -eq $Null
} | ForEach-Object {
$jobs.remove($_)
}
Start-Sleep -Seconds 1
} while ($jobCleanup.Flag)
})
$jobCleanup.PowerShell.Runspace = $newRunspace
$jobCleanup.Thread = $jobCleanup.PowerShell.BeginInvoke()
#endregion Background runspace to clean up jobs
$syncHash.textDomain.Text = $env:USERDOMAIN
$syncHash.textFileShare.Text = (Get-Location) | ForEach-Object{$_.ProviderPath}
#region Workload Button
$syncHash.buttonWorkload.Add_Click({
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = (Get-Location) | ForEach-Object{$_.ProviderPath}
Multiselect = $false
Filter = 'Powershell (*.PS1)|*.PS1'
}
$null = $FileBrowser.ShowDialog()
$syncHash.Window.Dispatcher.invoke(
[action]{
$syncHash.textWorkload.Text = $FileBrowser.FileName
"Normal"
})
})
#endregion
#region Start Button
$syncHash.buttonStart.Add_Click({
$global:testDate = (Get-Date -format filedatetime)
$x+= "."
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Name = "StartTest"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("testDate",$global:testDate)
$newRunspace.SessionStateProxy.SetVariable("SyncHash",$SyncHash)
$newRunspace.SessionStateProxy.SetVariable("jobs",$global:jobs)
$PowerShell = [PowerShell]::Create().AddScript({
$syncHash.Window.Dispatcher.invoke(
[action]{
$script:VDAs = $syncHash.textVDAs.Text.split()
$script:Launchers = $syncHash.textLaunchers.Text.split()
$script:Users = $syncHash.textUsers.Text.split()
$script:Desktop = $syncHash.textDesktop.Text
$script:StorefrontURL = $syncHash.textStorefrontURL.text
$script:Delay = $syncHash.textDelay.text
$script:Domain = $syncHash.textDomain.Text
$script:FileStore = $syncHash.textFileShare.Text
$script:Perfmon = $syncHash.checkPerfmon.isChecked
$script:Workload = $syncHash.textWorkload.Text
$script:enabled = $syncHash.textWorkload.isEnabled
$syncHash.listViewSessions.items.Clear()
},
"Normal"
)
import-module $script:filestore\scripts\ControlFunctions.psm1
#Write-Log -StartNew -Path $script:filestore\logs\event.log
Write-Log -Message "Start Button Clicked" -Level Info -Path $script:filestore\logs\event.log
$filestore = $script:FileStore
set-location $filestore
Test-Inputs -delay $script:Delay -filestore $script:filestore -storefrontURL $script:StorefrontURL -VDAs $script:VDAs -users $script:Users -launchers $script:launchers -date $testDate -workload $script:Workload -desktop $script:Desktop
$script:intDelay = [int]$script:Delay
Watch-LauncherStatus -filestore $script:filestore -launchers $script:Launchers -launcherscount $script:Launchers.count
If($script:perfmon -eq $true){Start-Perfmon -date $testDate -VDAs $Script:VDAs -filestore $script:filestore}
Start-ProgressBar -intDelay $script:intDelay -UserCount $Script:Users.count -filestore $script:filestore
publish-test -date $testDate -filestore $script:FileStore -launchers $script:Launchers -VDAs $script:VDAs -workload $script:Workload -desktop $script:Desktop -intdelay $script:intDelay -storefrontURL $script:StorefrontURL -users $Script:Users
Start-SessionInfo -VDAs $Script:VDAs -filestore $script:filestore
Watch-TestStatus -filestore $script:filestore -launchers $script:Launchers -delay $script:intDelay
Unpublish-test -date $testDate -filestore $script:FileStore -launchers $script:Launchers -VDAs $script:VDAs -workload $script:Workload
})
$PowerShell.Runspace = $newRunspace
[void]$global:jobs.Add((
[pscustomobject]@{
PowerShell = $PowerShell
Runspace = $PowerShell.BeginInvoke()
}
))
})
#endregion
#region Stop Button
$syncHash.buttonStop.Add_Click({
$x+= "."
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunSpace.name = "StopTest"
$newRunspace.SessionStateProxy.SetVariable("SyncHash",$SyncHash)
$newRunspace.SessionStateProxy.SetVariable("testDate",$global:testDate)
$PowerShell = [PowerShell]::Create().AddScript({
$syncHash.Window.Dispatcher.invoke(
[action]{
$script:VDAs = $syncHash.textVDAs.Text.split()
$script:Launchers = $syncHash.textLaunchers.Text.split()
$script:Users = $syncHash.textUsers.Text.split()
$script:Desktop = $syncHash.textDesktop.Text
$script:StorefrontURL = $syncHash.textStorefrontURL.text
$script:Delay = $syncHash.textDelay.text
$script:Domain = $syncHash.textDomain.Text
$script:Filestore = $syncHash.textFileShare.Text
$script:Workload = $syncHash.textWorkload.Text
},
"Normal"
)
$filestore = $script:filestore
import-module $script:filestore\scripts\ControlFunctions.psm1
Write-Log -Message "Stop Button Clicked" -Level Info -Path $script:filestore\logs\event.log
Get-Runspace -name "StartTest" | ForEach-Object {$_.dispose()}
unpublish-test -date $testDate -filestore $script:FileStore -launchers $script:Launchers -VDAs $script:VDAs -workload $script:Workload
})
$PowerShell.Runspace = $newRunspace
[void]$global:jobs.Add((
[pscustomobject]@{
PowerShell = $PowerShell
Runspace = $PowerShell.BeginInvoke()
}
))
})
#endregion
#region Window Close
$syncHash.Window.Add_Closed({
Write-Verbose 'Halt runspace cleanup job processing'
$jobCleanup.Flag = $False
#Stop all runspaces
$jobCleanup.PowerShell.Dispose()
Get-Runspace | Where-Object {$_.id -ne 1} | ForEach-Object {$_.dispose()}
Write-Log -Message "Program Close" -Level Info -Path $script:filestore\logs\event.log
})
#endregion Window Close
$syncHash.Window.ShowDialog() | Out-Null
$syncHash.Error = $Error
})
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()
#this is a hacky way of keeping the GUI open when run from Console
#do{$something = 0}
#until($something -eq 1)
#Get-Runspace | Where-Object {$_.id -ne 1} | ForEach-Object {$_.dispose()}