-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDirect2Events.ps1
2510 lines (2296 loc) · 110 KB
/
Direct2Events.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires -version 3.0
<#
Use multiple vendor's PowerShell modules to centralise information for XenApp/XenDesktop 7.x and allow troubleshooting and investigation all from one UI
Guy Leech, 2018
Modification History:
22/05/18 GL Added "Find User" functionality
#>
[CmdletBinding()]
Param
(
[string]$configRegKey = 'HKCU:\software\Guy Leech\Citrix Consolidated Config' ,
[string[]]$snapins = @( 'Citrix.Broker.Admin.*' ) ,
[string[]]$modules = @( 'ActiveDirectory' , 'VMware.VimAutomation.Core' , "$env:ProgramFiles\Citrix\Provisioning Services Console\Citrix.PVS.SnapIn.dll" , 'Guys.Common.Functions.psm1' ) ,
[int]$maxRecordCount = 1000 ,
[string]$configFile ,
[switch]$alwaysOnTop ,
[int]$secondsWindow = 120 , ## default but overrriden by config/registry
[string]$messageText = 'Please logoff' ,
[string]$actionPattern = '##Input' ,
## Read from registry or configuration dialog
[string]$ddc = $null ,
[string]$PVS = $null ,
[string]$AMC = $null ,
[string[]]$hypervisor = @() ,
[switch]$test
)
$global:hypervisorConnected = $false
[hashtable]$processPriorities =
@{
24 = 'Realtime'
13 = 'High'
10 = 'Above normal'
8 = 'Normal'
6 = 'Below normal'
4 = 'Low'
}
$setworkingsetPinvoke = @'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PInvoke.Win32
{
public static class Memory
{
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetProcessWorkingSetSizeEx( IntPtr proc, int min, int max , int flags );
}
}
'@
#region XAML&Modules
$machineSelectionWindowXML = @"
<Window x:Class="Direct2Events.MachineFilter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Machine Filter" Height="757" Width="414">
<Grid Height="307" VerticalAlignment="Top">
<ComboBox x:Name="comboDeliveryGroup" HorizontalAlignment="Left" Height="26" Margin="154,33,0,0" VerticalAlignment="Top" Width="210"/>
<CheckBox x:Name="chkDeliveryGroup" Content="Delivery Group" HorizontalAlignment="Left" Height="26" Margin="10,37,0,0" VerticalAlignment="Top" Width="104"/>
<ComboBox x:Name="comboMachineCatalogue" HorizontalAlignment="Left" Height="27" Margin="154,76,0,0" VerticalAlignment="Top" Width="210"/>
<CheckBox x:Name="chkMachineCatalogue" Content="Machine Catalogue" HorizontalAlignment="Left" Height="20" Margin="11,83,0,0" VerticalAlignment="Top" Width="127"/>
<CheckBox x:Name="chkMultiSession" Content="Multi Session
" HorizontalAlignment="Left" Height="18" Margin="12,133,0,0" VerticalAlignment="Top" Width="102" IsChecked="True"/>
<CheckBox x:Name="chkSingleSession" Content="Single Session" HorizontalAlignment="Left" Height="27" Margin="10,183,0,0" VerticalAlignment="Top" Width="117"/>
<Button x:Name="btnConfigurationOk" Content="OK" HorizontalAlignment="Left" Height="22" Margin="19,691,0,-406" VerticalAlignment="Top" Width="77" IsDefault="True"/>
<Button x:Name="btnConfigurationCancel" Content="Cancel" HorizontalAlignment="Left" Height="22" Margin="114,691,0,-406" VerticalAlignment="Top" Width="77" IsCancel="True"/>
<CheckBox x:Name="chkPoweredOn" Content="Powered On" HorizontalAlignment="Left" Height="24" Margin="10,222,0,0" VerticalAlignment="Top" Width="113"/>
<CheckBox x:Name="chkMachineName" Content="Machine Name" HorizontalAlignment="Left" Height="22" Margin="11,265,0,0" VerticalAlignment="Top" Width="118"/>
<TextBox x:Name="txtMachineName" HorizontalAlignment="Left" Height="28" Margin="164,259,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
<CheckBox x:Name="chkUsersConnected" Content="With users connected" HorizontalAlignment="Left" Height="25" Margin="10,413,0,-131" VerticalAlignment="Top" Width="232"/>
<StackPanel Margin="12,453,170.333,-218" Orientation="Vertical" Name="stkMaintenanceMode">
<RadioButton x:Name="radInMaintenanceMode" Content="In Maintenance Mode" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="174" GroupName="MaintenanceMode"/>
<RadioButton x:Name="radNotInMaintenanceMode" Content="Not in Maintenance Mode" HorizontalAlignment="Left" Height="21" VerticalAlignment="Top" Width="201" GroupName="MaintenanceMode"/>
<RadioButton x:Name="radEitherMaintenanceMode" Content="Either" HorizontalAlignment="Left" Height="17" VerticalAlignment="Top" Width="151" GroupName="MaintenanceMode" IsChecked="True"/>
</StackPanel>
<StackPanel Margin="164,307,40.333,-78" Orientation="Vertical" Name="stkMachinesFrom">
<RadioButton x:Name="radMachinesFromCitrix" Content="Citrix" HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="179" IsChecked="True" GroupName="MachinesFrom"/>
<RadioButton x:Name="radMachinesFromHypervisor" Content="Hypervisor" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="179" GroupName="MachinesFrom"/>
<RadioButton x:Name="radMachinesFromAD" Content="Active Directory" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="155" GroupName="MachinesFrom"/>
</StackPanel>
<Label Content="Retrieve from:" HorizontalAlignment="Left" Height="32" Margin="45,335,0,-60" VerticalAlignment="Top" Width="93"/>
<StackPanel x:Name="stkRegistrationState" Margin="12,541,164,-315" Orientation="Vertical">
<RadioButton x:Name="radRegistered" Content="Registered" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="230" GroupName="Registered"/>
<RadioButton x:Name="radUnregistered" Content="Unregistered" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="230" GroupName="Registered"/>
<RadioButton x:Name="radEitherRegisteredUnregistered" Content="Either" HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="224" IsChecked="True" GroupName="Registered"/>
</StackPanel>
</Grid>
</Window>
"@
$progressWindowXML = @"
<Window x:Name="Progress" x:Class="Direct2Events.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Busy ..." Height="142.389" Width="303.854">
<Grid>
<TextBox x:Name="txtProgress" HorizontalAlignment="Left" Height="67" Margin="10,21,0,0" TextWrapping="Wrap" Text="Doing stuff ..." VerticalAlignment="Top" Width="270"/>
</Grid>
</Window>
"@
$configurationWindowXML = @"
<Window x:Class="Direct2Events.Configuration"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Configuration" Height="523.333" Width="343.511">
<Grid Margin="0,0,0,0 ">
<TextBox x:Name="txtDDC" HorizontalAlignment="Left" Height="26" Margin="160,30,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160"/>
<Label Content="Delivery Controllers" HorizontalAlignment="Left" Height="26" Margin="14,30,0,0" VerticalAlignment="Top" Width="124" AutomationProperties.Name="Delivery Controllers"/>
<TextBox x:Name="txtPVS" HorizontalAlignment="Left" Height="26" Margin="160,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160"/>
<Label Content="PVS Servers" HorizontalAlignment="Left" Height="26" Margin="14,108,0,0" VerticalAlignment="Top" Width="124" AutomationProperties.Name="Delivery Controllers"/>
<TextBox x:Name="txtHypervisor" HorizontalAlignment="Left" Height="26" Margin="160,161,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160"/>
<Label Content="Hypervisor" HorizontalAlignment="Left" Height="26" Margin="14,161,0,0" VerticalAlignment="Top" Width="124" AutomationProperties.Name="Delivery Controllers"/>
<TextBox x:Name="txtAppSense" HorizontalAlignment="Left" Height="26" Margin="160,205,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160" />
<Label Content="AppSense" HorizontalAlignment="Left" Height="26" Margin="14,205,0,0" VerticalAlignment="Top" Width="124" AutomationProperties.Name="Delivery Controllers"/>
<Button x:Name="btnConfigurationOk" Content="OK" HorizontalAlignment="Left" Height="22" Margin="19,452,0,0" VerticalAlignment="Top" Width="77"/>
<Button x:Name="btnConfigurationCancel" Content="Cancel" HorizontalAlignment="Left" Height="22" Margin="114,452,0,0" VerticalAlignment="Top" Width="77"/>
<Grid Margin="19,205,198.667,241">
<TextBox x:Name="txtSeconds" Height="24" Margin="-3,174,74.333,-152" TextWrapping="Wrap" Text="120" VerticalAlignment="Top" UndoLimit="99"/>
<Label x:Name="labelSeconds" Content="Seconds before/after" HorizontalAlignment="Left" Height="28" Margin="50,170,-167,-150" VerticalAlignment="Top" Width="236"/>
</Grid>
<CheckBox x:Name="chkHttps" Content="https" HorizontalAlignment="Left" Height="21" Margin="160,74,0,0" VerticalAlignment="Top" Width="59"/>
<TextBox x:Name="txtMessageText" HorizontalAlignment="Left" Height="104" Margin="160,261,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160"/>
<Label Content="MessageText" HorizontalAlignment="Left" Height="27" Margin="14,261,0,0" VerticalAlignment="Top" Width="128"/>
</Grid>
</Window>
"@
$healthWindowXML = @"
<Window x:Name="HealthReportWindow" x:Class="Direct2Events.HealthReport"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Information" Height="540.178" Width="794.03">
<Grid Margin="0,0,-209.667,-5.333" HorizontalAlignment="Stretch">
<StackPanel HorizontalAlignment="Left" Height="458" Margin="32,20,0,0" VerticalAlignment="Top" Width="730">
<ListView x:Name="Health" HorizontalAlignment="Left" Height="454" VerticalAlignment="Top" Width="730" >
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Remediate" Name="HealthInfoContextMenu" />
</ContextMenu>
</ListView.ContextMenu>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="Blue" />
</Trigger>
<DataTrigger Binding="{Binding Path=Warning}" Value="True">
<Setter Property="Foreground" Value="Orange" />
<Setter Property="Background" Value="DarkBlue" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Critical}" Value="True">
<Setter Property="Foreground" Value="Red" />
<Setter Property="Background" Value="DarkBlue" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Property" DisplayMemberBinding ="{Binding Name}"/>
<GridViewColumn Header="Value" DisplayMemberBinding ="{Binding Value}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Grid>
</Window>
"@
$processesWindowXML = @'
<Window x:Class="Direct2Events.ProcessList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Processes" Height="300" Width="600">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Name="ProcessList">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Trim" Name="TrimProcessContextMenu" />
<MenuItem Header="Set Maximum Working Set" Name="SetMaxWorkingSetProcessContextMenu" />
<MenuItem Header="Kill" Name="KillProcessContextMenu" />
<MenuItem Header="Set Priority" Name="SetPriorityProcessContextMenu">
<MenuItem Header="High" Name="SetHighPriorityProcessContextMenu" />
<MenuItem Header="Above Normal" Name="SetAboveNormalPriorityProcessContextMenu" />
<MenuItem Header="Normal" Name="SetNormalPriorityProcessContextMenu" />
<MenuItem Header="Below Normal" Name="SetBelowNormalPriorityProcessContextMenu" />
<MenuItem Header="Low" Name="SetLowPriorityProcessContextMenu" />
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding ProcessId}" Header="PID"/>
<DataGridTextColumn Binding="{Binding ParentProcessId}" Header="Parent Process Id"/>
<DataGridTextColumn Binding="{Binding SessionId}" Header="Session Id"/>
<DataGridTextColumn Binding="{Binding Owner}" Header="Owner"/>
<DataGridTextColumn Binding="{Binding WorkingSetKB}" Header="Working Set (KB) "/>
<DataGridTextColumn Binding="{Binding PeakWorkingSetKB}" Header="Peak Working Set (KB)"/>
<DataGridTextColumn Binding="{Binding PageFileUsageKB}" Header="Page File Usage (KB)"/>
<DataGridTextColumn Binding="{Binding PeakPageFileUsageKB}" Header="Peak Page File Usage (KB)"/>
<DataGridTextColumn Binding="{Binding IOReadsMB}" Header="IO Reads (MB)"/>
<DataGridTextColumn Binding="{Binding IOWritesMB}" Header="IO Writes (MB)"/>
<DataGridTextColumn Binding="{Binding BasePriority}" Header="Base Priority"/>
<DataGridTextColumn Binding="{Binding ProcessorTime}" Header="Processor Time (s)"/>
<DataGridTextColumn Binding="{Binding HandleCount}" Header="Handles"/>
<DataGridTextColumn Binding="{Binding ThreadCount}" Header="Threads"/>
<DataGridTextColumn Binding="{Binding StartTime}" Header="Start Time"/>
<DataGridTextColumn Binding="{Binding CommandLine}" Header="Command Line"/>
</DataGrid.Columns>
</DataGrid>
<TextBlock Grid.Row="1">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding ElementName="grid" Path="ActualWidth"/>
<Binding ElementName="grid" Path="ActualHeight"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Window>
'@
$mainWindowXML = @"
<Window x:Class="Direct2Events.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Citrix Centralised Console" Height="662.256" Width="1250">
<Grid Margin="0,0,-21.667,6.667">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="53*"/>
<ColumnDefinition Width="7*"/>
<ColumnDefinition Width="458*"/>
</Grid.ColumnDefinitions>
<DatePicker x:Name="startDatePicker" HorizontalAlignment="Left" Margin="71,30,0,0" VerticalAlignment="Top" Grid.ColumnSpan="3"/>
<Button x:Name="btnGetUsers" Content="Get _Users" HorizontalAlignment="Left" Height="25" Margin="24,126,0,0" VerticalAlignment="Top" Width="101" Grid.ColumnSpan="3" IsDefault="True"/>
<ListView x:Name="Users" HorizontalAlignment="Left" Height="495" Margin="103.333,63,0,0" VerticalAlignment="Top" Width="284" Grid.Column="2" SelectionMode="Single">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Info" Name="UserUserInfoContextMenu" />
<MenuItem Header="Events" >
<MenuItem Header="Events in Time Range" Name="EventsInRangeContextMenu" />
<MenuItem Header="Boot Events" Name="MachineBootEventsContextMenu" />
</MenuItem>
<MenuItem Header="Actions" >
<MenuItem Header="Logoff" Name="MachineLogoffContextMenu" />
<MenuItem Header="Message" Name="MachineMessageContextMenu" />
<MenuItem Header="Disconnect" Name="MachineDisconnectContextMenu" />
<MenuItem Header="Restart Service" Name="MachineRestartServiceContextMenu" />
<MenuItem Header="Maintenance Mode" Name="MachineMaintenanceModeContextMenu" />
<MenuItem Header="Reboot" Name="MachineRebootContextMenu" />
<MenuItem Header="Logon" Name="MachineLogonContextMenu" />
</MenuItem>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding ="{Binding 'Value'}" />
<GridViewColumn Header="Active Users" DisplayMemberBinding ="{Binding 'Value2'}" />
<GridViewColumn Header="Boot Time" DisplayMemberBinding ="{Binding 'Value3'}" />
</GridView>
</ListView.View>
</ListView>
<Label x:Name="labelUsers" Content="Users" HorizontalAlignment="Left" Height="27" Margin="103.333,27,0,0" VerticalAlignment="Top" Width="284" Grid.Column="2"/>
<ListView x:Name="Sessions" HorizontalAlignment="Left" Height="496" Margin="508.333,61,0,0" VerticalAlignment="Top" Width="421" Grid.Column="2" SelectionMode="Single" >
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Information" >
<MenuItem Header="User Info" Name="UserInfoContextMenu" />
<MenuItem Header="Machine Info" Name="MachineInfoContextMenu" />
<MenuItem Header="Session Info" Name="SessionInfoContextMenu" />
<MenuItem Header="Processes">
<MenuItem Header="Session" Name="SessionProcessesContextMenu" />
<MenuItem Header="All" Name="AllProcessesContextMenu" />
</MenuItem>
</MenuItem>
<MenuItem Header="Events" >
<MenuItem Header="Logon Events" Name="LogonEventsContextMenu" />
<MenuItem Header="Logoff Events" Name="LogoffEventsContextMenu" />
<MenuItem Header="Entire Session Events" Name="AllSessionEventsContextMenu" />
<MenuItem Header="Boot Events" Name="BootEventsContextMenu" />
<MenuItem Header="Events in Time Range" Name="EventsInRangeUserContextMenu" />
</MenuItem>
<MenuItem Header="Actions" >
<MenuItem Header="Logoff" Name="LogoffContextMenu" />
<MenuItem Header="Message" Name="MessageContextMenu" />
<MenuItem Header="Shadow" Name="ShadowContextMenu" />
<MenuItem Header="Disconnect" Name="DisconnectContextMenu" />
<MenuItem Header="Restart Service" Name="RestartServiceContextMenu" />
<MenuItem Header="Maintenance Mode" Name="MaintenanceModeContextMenu" />
<MenuItem Header="Reboot" Name="RebootContextMenu" />
<MenuItem Header="Logon" Name="LogonContextMenu" />
</MenuItem>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView>
<GridViewColumn Header="Start" DisplayMemberBinding ="{Binding 'Column1'}" Width="150" />
<GridViewColumn Header="End" DisplayMemberBinding ="{Binding 'Column2'}" Width="150" />
<GridViewColumn Header="Server" DisplayMemberBinding ="{Binding 'Column3'}" Width="100"/>
</GridView>
</ListView.View>
<ListView x:Name="listView" Height="100" Width="100">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
</ListView>
<Label x:Name="labelSessions" Content="Sessions" HorizontalAlignment="Left" Height="27" Margin="508.333,29,0,0" VerticalAlignment="Top" Width="116" Grid.Column="2"/>
<Button x:Name="btnSettings" Content="Settings" HorizontalAlignment="Left" Height="25" Margin="24,321,0,0" VerticalAlignment="Top" Width="101" AutomationProperties.Name="Settings"/>
<Label Content="Start" HorizontalAlignment="Left" Margin="17,29,0,0" VerticalAlignment="Top"/>
<DatePicker x:Name="endDatePicker" HorizontalAlignment="Left" Margin="71,80,0,0" VerticalAlignment="Top" Grid.ColumnSpan="3" Width="101"/>
<Label Content="End" HorizontalAlignment="Left" Margin="24,78,0,0" VerticalAlignment="Top"/>
<Button x:Name="btnFindUser" Grid.ColumnSpan="3" Content="Find User" HorizontalAlignment="Left" Height="25" Margin="24,172,0,0" VerticalAlignment="Top" Width="101"/>
<Button x:Name="btnGetMachines" Grid.ColumnSpan="3" Content="Get _Machines" HorizontalAlignment="Left" Height="25" Margin="24,220,0,0" VerticalAlignment="Top" Width="101"/>
<Button x:Name="btnTest" Content="Test" HorizontalAlignment="Left" Height="25" Margin="24,383,0,0" VerticalAlignment="Top" Width="101"/>
<Button x:Name="btnPVSDevices" Content="PVS Devices" HorizontalAlignment="Left" Height="25" Margin="24,268,0,0" VerticalAlignment="Top" Width="101"/>
</Grid>
</Window>
"@
$generalTextEntry = @'
<Window x:Name="EnterText" x:Class="Direct2Events.EnterText"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Enter Text" Height="212.986" Width="292.038">
<Grid FocusManager.FocusedElement="{Binding ElementName=textBoxEnterText}">
<TextBox x:Name="textBoxEnterText" HorizontalAlignment="Left" Height="29" Margin="30,46,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="227" MaxLines="1"/>
<Button x:Name="btnTextEntryOk" Content="OK" HorizontalAlignment="Left" Height="24" Margin="30,97,0,0" VerticalAlignment="Top" Width="76" IsDefault="True"/>
<Button x:Name="btnTextEntryCancel" Content="Cancel" HorizontalAlignment="Left" Height="24" Margin="126,97,0,0" VerticalAlignment="Top" Width="71" IsCancel="True"/>
</Grid>
</Window>
'@
$messageWindowXML = @"
<Window x:Class="Direct2Events.MessageWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Send Message" Height="414.667" Width="309.333">
<Grid>
<TextBox x:Name="txtMessageCaption" HorizontalAlignment="Left" Height="53" Margin="85,20,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="180"/>
<Label Content="Caption" HorizontalAlignment="Left" Height="24" Margin="10,20,0,0" VerticalAlignment="Top" Width="63"/>
<TextBox x:Name="txtMessageBody" HorizontalAlignment="Left" Height="121" Margin="85,171,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="180"/>
<Label Content="Message" HorizontalAlignment="Left" Height="25" Margin="10,167,0,0" VerticalAlignment="Top" Width="56" RenderTransformOrigin="0.47,1.333"/>
<StackPanel Orientation="Horizontal" Height="43" Margin="10,332,0,0" Width="283">
<Button x:Name="btnMessageOk" Content="OK" Height="20" Width="89"/>
<Button x:Name="btnMessageCancel" Content="Cancel" Height="20" Margin="50,0,0,0" Width="89"/>
</StackPanel>
<ComboBox x:Name="comboMessageStyle" HorizontalAlignment="Left" Height="27" Margin="85,98,0,0" VerticalAlignment="Top" Width="180">
<ComboBoxItem Content="Information" IsSelected="True"/>
<ComboBoxItem Content="Question"/>
<ComboBoxItem Content="Exclamation"/>
<ComboBoxItem Content="Critical"/>
</ComboBox>
<Label Content="Level" HorizontalAlignment="Left" Height="27" Margin="15,98,0,0" VerticalAlignment="Top" Width="58"/>
</Grid>
</Window>
"@
$datePickerXAML = @"
<Window x:Name="DatePicker" x:Class="Direct2Events.DatePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Pick a date, any date" Height="196" Width="286">
<Grid Margin="0,0,36,11">
<DatePicker x:Name="datePicked" HorizontalAlignment="Left" Height="50" Margin="13,21,0,0" VerticalAlignment="Top" Width="221"/>
<Button x:Name="btnDatePickerOK" Content="OK" HorizontalAlignment="Left" Height="18" Margin="13,116,0,-10" VerticalAlignment="Top" Width="71" IsDefault="True"/>
<Button x:Name="btnDatePickerCancel" Content="Cancel" HorizontalAlignment="Left" Height="18" Margin="103,116,0,-10" VerticalAlignment="Top" Width="66" IsCancel="True"/>
<StackPanel Orientation="Horizontal" Height="20" Margin="13,82,17,0" VerticalAlignment="Top">
<TextBlock HorizontalAlignment="Left" Height="19" Margin="3,0,0,0" TextWrapping="Wrap" Width="31" Text="Time"/>
<TextBox x:Name="txtTime" Height="19" TextWrapping="Wrap" Text="00:00:00" Width="129" Margin="39,0"/>
</StackPanel>
</Grid>
</Window>
"@
$passwordPickerXAML = @"
<Window x:Name="PasswordPicker" x:Class="Direct2Events.PasswordPicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="PasswordPicker" Height="287.634" Width="297.71">
<Grid Margin="0,0,0,143">
<PasswordBox x:Name="passwordBox" HorizontalAlignment="Left" Height="33" Margin="26,27,0,0" VerticalAlignment="Top" Width="219"/>
<Button x:Name="btnPasswordOk" Content="OK" HorizontalAlignment="Left" Height="31" Margin="26,80,0,0" VerticalAlignment="Top" Width="95" IsDefault="True"/>
<Button x:Name="btnPasswordCancel" Content="Cancel" HorizontalAlignment="Left" Height="31" Margin="150,80,0,0" VerticalAlignment="Top" Width="79" IsCancel="True"/>
</Grid>
</Window>
"@
function Global:Invoke-ODataTransform ($records )
{
if( $records )
{
$propertyNames = ($records | Select -First 1).content.properties |
Get-Member -MemberType Properties |
Select -ExpandProperty name
[int]$timeOffset = Get-DayLightSavingsOffet
$records | ForEach-Object `
{
$record = $_
$h = @{}
$h.ID = $record.ID
$properties = $record.content.properties
$propertyNames | ForEach-Object `
{
$propertyName = $_
$targetProperty = $properties.$propertyName
if($targetProperty -is [Xml.XmlElement])
{
try
{
$h.$propertyName = $targetProperty.'#text'
## see if we need to adjust for daylight savings
if( $timeOffset -and ! [string]::IsNullOrEmpty( $h.$propertyName ) -and $targetProperty.type -match 'DateTime' )
{
$h.$propertyName = (Get-Date -Date $h.$propertyName).AddHours( $timeOffset )
}
}
catch
{
##$_
}
}
else
{
$h.$propertyName = $targetProperty
}
}
[PSCustomObject]$h
}
}
}
Function Get-DayLightSavingsOffet
{
[OutputType([Int])]
Param()
if( (Get-Date).IsDaylightSavingTime() )
{
1
}
else
{
0
}
}
Function Progress-Window( [string]$text , $parent )
{
if( [string]::IsNullOrEmpty( $text ) )
{
## Called to close progress window
if( $global:job )
{
##$global:progressWindow.Close()
$global:form.Close()
$global:newThread.Stop()
$global:newThread.Dispose()
$global:powershell.Dispose()
}
$global:runspace.Close()
$global:powershell.Dispose()
}
else
{
Add-Type -AssemblyName System.Windows.Forms
## Create async message box to inform user what we're doing
$global:form = New-Object System.Windows.Forms.Form
if( $parent )
{
$parent.AddChild( $form )
}
$form.Size = New-Object System.Drawing.Size(300,150)
$form.StartPosition = 'CenterParent'
$form.Location = New-Object Drawing.Point 50 , ( ([Windows.Forms.Screen]::PrimaryScreen).WorkingArea.Height - 400 ) ## want it near the start menu
$form.AutoSize = $true
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object Drawing.Point 10,20
$label.Size = New-Object System.Drawing.Point 250,60
$label.AutoSize = $true
$label.Font = New-Object System.Drawing.Font( $label.Font.Name, 12 , [Drawing.FontStyle]::Bold ) ## 12 is the point size, default is usualy too small
$form.Controls.Add( $label )
$form.TopMost = $true
$label.text = $text
$form.text = "Busy ..."
$progress = { [void]$form.ShowDialog() }
##$global:progressWindow = Load-GUI $progressWindowXML
if( $true ) ##$global:progressWindow )
{
##$WPFtxtProgress.Text = $text
$global:powershell = [powershell]::Create()
$global:runspace = [runspacefactory]::CreateRunspace()
$global:runspace.Open()
$global:newThread = $powershell.AddScript($progress)
$global:runspace.SessionStateProxy.SetVariable( 'form' , $form ) # $global:progressWindow )
$global:powershell.Runspace = $global:runspace
$global:job = $global:newThread.BeginInvoke() ## This will cause the form to be displayed
}
}
}
Function Load-GUI( $inputXml )
{
$form = $NULL
$inputXML = $inputXML -replace 'mc:Ignorable="d"' , '' -replace 'x:N' ,'N' -replace '^<Win.*' , '<Window'
[xml]$XAML = $inputXML
$reader = New-Object Xml.XmlNodeReader $xaml
try
{
$Form = [Windows.Markup.XamlReader]::Load( $reader )
}
catch
{
Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .NET is installed.`n$_"
return $null
}
$xaml.SelectNodes('//*[@Name]') | ForEach-Object `
{
Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -Scope Global
}
return $form
}
[void][Reflection.Assembly]::LoadWithPartialName('Presentationframework')
$mainForm = Load-GUI $mainWindowXML
if( ! $mainForm )
{
return
}
if( $DebugPreference -eq 'Inquire' )
{
Get-Variable -Name WPF*
}
if( $snapins -and $snapins.Count -gt 0 )
{
ForEach( $snapin in $snapins )
{
Add-PSSnapin $snapin
}
}
if( $modules -and $modules.Count -gt 0 )
{
ForEach( $module in $modules )
{
Import-Module $module -ErrorAction SilentlyContinue
[bool]$loaded = $?
if( ! $loaded -and $module -notmatch '^[a-z]:\\' -and $module -notmatch '^\\\\' ) ## only check script folder if not an absolute or UNC path
{
## try same folder as the script if there is no path in the module name
Import-Module (Join-Path ( & { Split-Path -Path $myInvocation.ScriptName -Parent } ) $module ) -ErrorAction Continue
$loaded = $?
}
if( ! $loaded )
{
Write-Warning "Unable to load module `"$module`" so functionality may be limited"
}
}
}
#endregion XAML&Modules
Function Action-Item( $item )
{
## item is what was pulled from the csv: RightClickName,RightClickAction,RightClickInputs
## e.g. "Extend Account,Set-ADAccountExpiration $value -DateTime ##Input1,$ XAML:Choose New Account Expiry Date"
## Where DatePickerXAML is XAML to produce UI to return data to replace placeholder ##Input
##
if( $item )
{
$value = $item.value
if( $item.RightClickAction -match $actionPattern )
{
if( $item.RightClickInputs )
{
$XAML,$controlWithResult,$header = $item.RightClickInputs -split ':'
$form = Load-GUI (Get-Variable $XAML).Value
if( $form )
{
$eventBlock = {
$_.Handled = $true
if( $_.psobject.Properties.name -match '^OriginalSource$' -and $_.OriginalSource.psobject.Properties.name -match '^content$' -and ($_.OriginalSource.Content -eq 'OK' -or $_.Source.OriginalSource -eq 'Cancel' ))
{
if( $_.OriginalSource.Content -eq 'OK' )
{
$this.DialogResult = $true
}
$this.Close()
}
}
$eventHandler = [Windows.RoutedEventHandler]$eventBlock
$form.AddHandler([Windows.Controls.Button]::ClickEvent, $eventHandler)
if( ! [string]::IsNullOrEmpty( $header ) )
{
$form.Title = $header
}
if( $form.ShowDialog() )
{
## Could look at name of form to figure what $WPFvariable to get or stuff it in the form somewhere
$result = Invoke-Expression ( '$WPF' + $controlWithResult )
$item.RightClickAction = $item.RightClickAction -replace "$($actionPattern)\d" , $result
}
else ## cancelled
{
return
}
}
else ## UI failed to load
{
## TODO error
return
}
}
else
{
## TODO Syntax error as with have an action pattern to replace but no info as to what to replce it with
return
}
}
## The command will probably need the user/machine as an argument
$user = $item.Owner
$computer = $item.Owner
$Error.Clear()
Invoke-Expression -Command $item.RightClickAction
if( $Error.Count -gt 0 )
{
[Windows.MessageBox]::Show( $Error[0].Exception.Message , "Failed to perform action `"$($item.RightClickAction)`"" , 'OK' ,'Error' )
}
}
}
Function Add-FromConfigFile( [string]$configFile , [string]$scope , $form , [string]$owner )
{
[hashtable]$actions = @{}
if( Test-Path $configFile )
{
[int]$line = 0
Import-Csv $configFile | Where-Object { $_.Scope -eq $scope } | ForEach-Object `
{
$line++
$configLine = $_
Write-Verbose "Config line $line : $_"
try
{
$value = Invoke-Expression $_.Value
## add null value if there is a warning or error associated
if( $_.Warning -or $_.Critical )
{
[bool]$warning = if( ! [string]::IsNullOrEmpty( $_.Warning ) ){ Invoke-Expression $_.Warning } else { $false }
[bool]$critical = if( ! [string]::IsNullOrEmpty( $_.Critical ) ){ Invoke-Expression $_.Critical } else { $false }
}
## if we have any remedation items then build them into a table so we can return them to caller, keyed on the property name
if( ![string]::IsNullOrEmpty( $_.RightClickName ) )
{
## Store value as doesn't seem to be a way to get it by the time the handler is called
$actions.Add( $_.Name , ( New-Object -TypeName PSCustomObject -Property (@{ 'Owner' = $owner ; 'Value' = $value ; 'RightClickName'=$_.RightClickName ; 'RightClickAction' = $_.RightClickAction ; 'RightClickInputs' = $_.RightClickInputs }) ) )
}
$form.AddChild( $( New-Object -TypeName PSCustomObject -Property (@{ 'Name' = $_.Name ; 'Value' = $value ; 'Warning' = $warning ; 'Critical' = $critical } )))
}
catch
{
Write-Warning "$configFile : $line : $configLine`n$_"
}
}
}
else
{
[Windows.MessageBox]::Show( "Failed to find config file `"$configFile`"" , "Machine Information" , 'OK' ,'Error' )
}
return $actions
}
Function Process-Processes( $GUIobject , [string]$Operation , [string]$machineName )
{
$_.Handled = $true
## get selected items from control
[int[]]$pids = @( $GUIobject.selectedItems | Select -ExpandProperty ProcessId )
if( ! $pids -or ! $pids.Count )
{
return
}
## prompt to confirm
$answer = [Windows.MessageBox]::Show( "Are you sure you want to $operation these $($pids.Count) processes?" , 'Confirm Action' , 'YesNo' ,'Question' )
if( $answer -ne 'yes' )
{
return
}
if( $operation -eq 'Kill' -or $operation -match 'Priority' )
{
[string]$method = if( $operation -eq 'Kill' ) { 'Terminate' } else { 'SetPriority' }
[int]$argument = switch( $operation ) ## See https://msdn.microsoft.com/en-us/library/aa393587(v=vs.85).aspx
{
'SetHighPriority' { 128 }
'SetAboveNormalPriority' { 32768 }
'SetNormalPriority' { 32 }
'SetBelowNormalPriority' { 16384 }
'SetLowPriority' { 64 }
'Kill' { 1 } ## exit code of process killed
}
$pids | ForEach-Object `
{
$thispid = $_
Get-WmiObject -Class win32_process -ComputerName $machineName -Filter "ProcessId ='$thispid'" | Invoke-WmiMethod -Name $method -ArgumentList $argument
}
}
else
{
[int]$maxWorkingSet = -1
[int]$minWorkingSet = -1
[int]$flags = 0
if( $operation -eq 'SetMaxWorkingSet' )
{
## Need to prompt for working set size to set
$limitForm = Load-GUI $generalTextEntry
if( $limitForm )
{
$wpfbtnTextEntryOk.Add_Click( {
$limitForm.DialogResult = $true
$limitForm.Close()
} )
$limitForm.Title = "Enter Maximum Working Set"
if( $limitForm.ShowDialog() )
{
$maxWorkingSet = Invoke-Expression $wpftextBoxEnterText.Text.Trim() ## in case MB or GB in there
$flags = $flags -bor 0x4 ## QUOTA_LIMITS_HARDWS_MAX_ENABLE
$minWorkingSet = 1 ## if a maximum is specified then we must specify a minimum too - this will default to the minimum
}
else
{
return
}
}
else
{
return
}
}
[scriptblock]$remoteCode = `
{
Add-Type $using:setworkingsetPinvoke
Get-Process -id $using:pids | ForEach-Object { [PInvoke.Win32.Memory]::SetProcessWorkingSetSizeEx( $_.Handle,$using:minWorkingSet,$using:maxWorkingSet,$using:flags) ; [ComponentModel.Win32Exception][Runtime.InteropServices.Marshal]::GetLastWin32Error() }
}
$result,$lastError = Invoke-Command -ComputerName $machineName -ScriptBlock $remoteCode
if( ! $result )
{
}
}
}
Function Show-UserInformation( [switch]$showProcesses = $false , [switch]$allProcesses )
{
## Need to see what is in the $WPFusers control -users or machines
if( $WPFlabelUsers.Content -and $WPFlabelUsers.Content -match 'users' )
{
$selectedUser = $WPFUsers.SelectedItem
}
else
{
$selectedUser = $WPFSessions.SelectedItem
}
if( $selectedUser )
{
[string]$userName = $selectedUser.Value
[string]$fullUserName = $null
if( $selectedUser.psobject.properties.name -match '^Name$' )
{
$fullUserName = $selectedUser.Name
}
else
{
$fullUserName = $selectedUser.'Full Name'
}
if( $showProcesses )
{
if( ! (Get-Member -InputObject $WPFSessions.SelectedItem -Name 'EndDate' -ErrorAction SilentlyContinue ) -or [string]::IsNullOrEmpty( $WPFSessions.SelectedItem.EndDate ) )
{
## get processes for this session and display with right click kill action
## TODO this won't work with multiple sessions for same user on same server
[string]$machine = $WPFSessions.SelectedItem.Machine
[string[]]$quser = (quser.exe $userName /server:$machine | select -Skip 1 -First 1).Trim() -split '\s+'
[int]$sessionId = -1
if( $quser -and $quser.Count )
{
if( $quser[0] -eq $userName )
{
[int]$index = 1
if( $quser[1] -match '^[^\d]' ) ## if starts with non-numeric then a winstation name so session not disconnected so session id is in next field
{
$index++
}
if( ! [int]::TryParse( $quser[$index] , [ref] $sessionId ) )
{
$sessionId = -1
Write-Warning "Unexpected text `"$($quser[$index]) found when looking for session id for user $userName on $machine"
}
}
else
{
Write-Warning "Unexpected user $($quser[0]) returned when looking for user $userName on $machine"
}
}
else
{
Write-Warning "Session not found when looking for user $userName on $machine"
}
if( $sessionId -ge 0 -or $allProcesses )
{
[string]$filter = if( $allProcesses ) { "sessionid >= 0" } else { "sessionid = '$sessionid'" }
## Process priorities: Realtime = 24, high = 13 , above normal = 10, normal = , below normal = 6, low = 4
## Some memory sizes are in KB and others not!
[array]$processes = @( Get-WmiObject -Class Win32_process -Filter $filter -ComputerName $machine | select Name,ProcessId,@{n='Owner';e={Invoke-WmiMethod -InputObject $_ -Name GetOwner | Select -ExpandProperty user}},ParentProcessId,SessionId,
@{n='WorkingSetKB';e={[math]::Round( $_.WorkingSetSize / 1KB)}},@{n='PeakWorkingSetKB';e={$_.PeakWorkingSetSize}},
@{n='PageFileUsageKB';e={$_.PageFileUsage}},@{n='PeakPageFileUsageKB';e={$_.PeakPageFileUsage}},
@{n='IOReadsMB';e={[math]::Round($_.ReadTransferCount/1MB)}},@{n='IOWritesMB';e={[math]::Round($_.WriteTransferCount/1MB)}},
@{n='BasePriority';e={$processPriorities[ [int]$_.Priority ] }},@{n='ProcessorTime';e={[math]::round( ($_.KernelModeTime + $_.UserModeTime) / 10e6 )}},HandleCount,ThreadCount,
@{n='StartTime';e={Get-Date ([Management.ManagementDateTimeConverter]::ToDateTime($_.CreationDate)) -Format G}},CommandLine )
if( $processes -and $processes.Count )
{
$processesForm = Load-GUI $processesWindowXML
if( $processesForm )
{
[string]$title = "$($processes.Count) processes $(if( ! $allProcesses ) { "for user $userName in session $sessionId " } )on $machine"
$processesForm.Title = $title
$WPFProcessList.ItemsSource = $processes
$WPFProcessList.IsReadOnly = $true
$WPFProcessList.CanUserSortColumns = $true
$WPFTrimProcessContextMenu.Add_Click({ Process-Processes -GUIobject $WPFProcessList -Operation 'Trim' -machineName $machine })
$WPFSetMaxWorkingSetProcessContextMenu.Add_Click({ Process-Processes -GUIobject $WPFProcessList -Operation 'SetMaxWorkingSet' -machineName $machine })
$WPFKillProcessContextMenu.Add_Click({ Process-Processes -GUIobject $WPFProcessList -Operation 'Kill' -machineName $machine })
$WPFSetHighPriorityProcessContextMenu.Add_Click({ Process-Processes -GUIobject $WPFProcessList -Operation 'SetHighPriority' -machineName $machine })
$WPFSetAboveNormalPriorityProcessContextMenu.Add_Click({ Process-Processes -GUIobject $WPFProcessList -Operation 'SetAboveNormalPriority' -machineName $machine })
$WPFSetNormalPriorityProcessContextMenu.Add_Click({ Process-Processes -GUIobject $WPFProcessList -Operation 'SetNormalPriority' -machineName $machine })
$WPFSetBelowNormalPriorityProcessContextMenu.Add_Click({ Process-Processes -GUIobject $WPFProcessList -Operation 'SetBelowNormalPriority' -machineName $machine })
$WPFSetLowPriorityProcessContextMenu.Add_Click({ Process-Processes -GUIobject $WPFProcessList -Operation 'SetLowPriority' -machineName $machine })
$processesForm.ShowDialog()
}
}
else
{
[Windows.MessageBox]::Show( "Got no processes in session $sessionId for user $userName on $machine" , 'Processes Information' , 'OK' ,'Warning' )
}
}
}
else
{
[Windows.MessageBox]::Show( "Session logged off at $($WPFSessions.SelectedItem.EndDate) so cannot get processes" , 'Processes Information' , 'OK' ,'Error' )
}
}
else ## user info requested rather than processes
{
$healthForm = Load-GUI $healthWindowXML
if( $healthForm )
{
$script:userActionRightClicked = $null
$WPFHealthInfoContextMenu.Add_Click({
Action-Item $script:userActionRightClicked
$_.Handled = $true
})
$healthForm.add_PreviewMouseRightButtonDown({
## $_.OriginalSource gives us the item so we can search config file we read in
## DataContext : @{Warning=False; Name=Email; [email protected]; Critical=False}
## Text : [email protected]
[string]$menuLabel = 'No action defined'
$script:userActionRightClicked = $null
if( $_ -and $_.OriginalSource -and $_.OriginalSource.DataContext )
{
$action = $script:userActions[ $_.OriginalSource.DataContext.Name ]
if( $action )
{
$menuLabel = $action.RightClickName
$script:userActionRightClicked = $action
}
}
$_.Source.ContextMenu.Items[0].Header = $menuLabel
$_.Handled = $true
})
$healthForm.Title = "$username Information"
$userDetails = Get-ADUser $userName -Properties *
if( $userDetails )
{
$script:userActions = Add-FromConfigFile -configFile $configFile -scope 'User' -form $WPFHealth -owner $userName
$healthForm.Show()
}
else
{
[Windows.MessageBox]::Show( "Failed to retrieve AD info for $username`n$($error[0])" , "Connection Information" , 'OK' ,'Error' )
}
}
}
}
}
Function Show-SessionInformation