-
Notifications
You must be signed in to change notification settings - Fork 28
/
MainWindow.xaml.cs
1065 lines (913 loc) · 39.9 KB
/
MainWindow.xaml.cs
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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Microsoft.UI;
using System.Text;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using System.Management.Automation;
using System.Xml.Linq;
using Microsoft.UI.Xaml.Media.Animation;
using Windows.UI;
using System.Reflection;
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Management.Automation.Runspaces;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using CsvHelper;
using System.Globalization;
using Microsoft.UI.Composition.SystemBackdrops;
using WinRT;
using System.Runtime.InteropServices;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace ITATKWinUI;
//public class MyTextClass : INotifyPropertyChanged
//{
// public event PropertyChangedEventHandler PropertyChanged;
// private string myText;
// public string MyText
// {
// get { return myText; }
// set
// {
// myText = value;
// OnPropertyChanged();
// }
// }
// protected void OnPropertyChanged([CallerMemberName] string name = null)
// {
// PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
// }
//}
public class ReportingRecord
{
public string ScriptName { get; set; }
public string ScriptPath { get; set; }
public DateTime DateTime { get; set; }
public string UserExecuting { get; set; }
public string HostExecuting { get; set; }
public string FileExtension { get; set; }
public string Target { get; set; }
}
public partial class MainWindow : Window
{
//TODO: Need a way to reset these as well as resetting the UI
public static string CurrentInput;
public static string CurrentMultipleInput;
private static string SingleOrMulti = "Single";
public static string MainTitle = "";
public static string MainSubTitle = "";
public Collection <Page> Pages = new();
public Collection <NavigationViewItem> NavigationViews = new();
public Frame cFrame => contentFrame;
public StackPanel mInputs => MachineInputs;
public NavigationView mNav => MainNav;
private readonly Type DashboardPage = typeof(Dashboard);
private readonly Type ReportingPage = typeof(Reporting);
private readonly Type SettingsPage = typeof(Settings);
private static void LaunchScript(object sender, EventArgs e, string scriptPath, string args, string type, string inputType, string wait, string elevate, string hide)
{
string EXEPath;
if(inputType == "Machine")
{
//TODO: This may need to be something like "-Machine CurrentInput" but for now this is fine
if (SingleOrMulti == "Single")
{
if (CurrentInput == "")
{
//TODO: Handle when there is no input but the config defines it as required
}
if (args != "")
{
args = args + " " + CurrentInput;
}
else
{
args = CurrentInput;
}
}
else
{
if (CurrentMultipleInput == "")
{
//TODO: Handle when there is no input but the config defines it as required
}
if (args != "")
{
args = args + " " + CurrentMultipleInput;
}
else
{
args = CurrentMultipleInput;
}
}
}
//TODO: Add VBS, WSF, BAT, CMD, and PY support
switch (type)
{
case "PS5":
EXEPath = @"powershell.exe";
break;
case "PS7":
EXEPath = @"pwsh.exe";
break;
default:
EXEPath = "";
break;
}
if (EXEPath != "")
{
if (!File.Exists(scriptPath))
{
scriptPath = Environment.CurrentDirectory + "\\" + scriptPath;
}
var process = new Process();
if (hide == "true")
{
process.StartInfo = new ProcessStartInfo("\"" + EXEPath + "\"", "-ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -File \"" + scriptPath + "\" " + args)
{
CreateNoWindow = false
};
} else {
process.StartInfo = new ProcessStartInfo("\"" + EXEPath + "\"", "-ExecutionPolicy Bypass -NoProfile -File \"" + scriptPath + "\" " + args)
{
CreateNoWindow = false
};
}
if(elevate == "true")
{
process.StartInfo.Verb = "runas";
process.StartInfo.UseShellExecute = true;
}
//process.StartInfo.RedirectStandardOutput = true;
//process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
//{
// // Prepend line numbers to each line of the output.
// if (!String.IsNullOrEmpty(e.Data))
// {
// lineCount++;
// output.Append("\n[" + lineCount + "]: " + e.Data);
// }
//});
if(wait == "true")
{
process.WaitForExit();
} else
{
process.Start();
}
//process.BeginOutputReadLine();
//process.WaitForExit();
//Debug.WriteLine(output);
//res = output.ToString();
//process.WaitForExit();
//process.Close();
}
}
private static void LaunchScript(string scriptPath, string args, string type, string inputType, string wait, string elevate, string hide)
{
//Overload condition for what we expect to use
LaunchScript(null, null, scriptPath, args, type, inputType, wait, elevate, hide);
}
private static void LaunchScript(string scriptPath, string args, string type, string inputType)
{
//Overload condition for what we expect to use
LaunchScript(null, null, scriptPath, args, type, inputType, "false", "no", "false");
}
private static void LaunchScript(string scriptPath, string type, string inputType)
{
//Overload condition if there are no args
LaunchScript(null,null,scriptPath, "", type, inputType, "false", "no", "false");
}
public static void LaunchExplorer(Object sender, EventArgs e, string path)
{
if (!File.Exists(path))
{
path = Environment.CurrentDirectory + "\\" + path;
}
var process = new Process
{
StartInfo = new ProcessStartInfo("\"explorer.exe\"", "\"" + path + "\"") //Should this open the folder or just launch the file? (effectively "exploring" the script in Notepad)
{
CreateNoWindow = false
}
};
process.Start();
}
public static void LaunchExplorer(string path)
{
LaunchExplorer(null, null, path);
}
public static Page GenerateCategoryPageFromXML(string name)
{
Page page = new Page();
page.Name = name;
//ResourceDictionary myResourceDictionary = new ResourceDictionary();
//myResourceDictionary.Source = new Uri("ResourceDictionary.xaml", UriKind.RelativeOrAbsolute);
////page.Resources.MergedDictionaries.Add(myResourceDictionary);
StackPanel stackPanel = new StackPanel();
stackPanel.Width = double.NaN;
stackPanel.Name = name;
TextBlock txtBlock = new TextBlock();
txtBlock.Text = name;
txtBlock.Padding = new Thickness(5);
txtBlock.FontSize = 24;
txtBlock.Margin = new Thickness(5);
stackPanel.Children.Add(txtBlock);
page.Content = stackPanel;
foreach (XElement item in from y in MainWindow.guiConfig.Descendants("Item") select y)
{
if(item.Attribute("category").Value == name)
{
stackPanel.Children.Add(MainWindow.GenerateExpanderFromXML(item.Attribute("name").Value, item.Attribute("description").Value, item.Attribute("path").Value, item.Attribute("psVersion").Value, item.Attribute("icon").Value, item.Attribute("category").Value, item.Attribute("inputType").Value));
}
}
return page;
}
//TODO: Fix the navigation search
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
// Only get results when it was a user typing,
// otherwise assume the value got filled in by TextMemberPath
// or the handler for SuggestionChosen.
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
//Set the ItemsSource to be your filtered dataset
//sender.ItemsSource = ;
}
}
private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
// Set sender.Text. You can use args.SelectedItem to build your text string.
}
private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if (args.ChosenSuggestion != null)
{
// User selected an item from the suggestion list, take an action on it here.
}
else
{
// Use args.QueryText to determine what to do.
}
}
public static NavigationViewItem GenerateCategoryNavigationViewItemFromXML(string category, string icon, string foreground)
{
NavigationViewItem navigationViewItem = new NavigationViewItem();
navigationViewItem.Content = category;
navigationViewItem.Name = "Nav" + category;
//navigationViewItem.Margin = new Thickness(10,0,10,0); //TODO: This looks great in expanded mode but we need to adjust it in collapsed mode
SymbolIcon symbolIcon = new SymbolIcon();
symbolIcon.Symbol = (Symbol)Enum.Parse(typeof(Symbol), icon);
//Auto set White/Black to opposite values if they match the active theme
if((foreground == "White") && (App.Current.RequestedTheme == ApplicationTheme.Light))
{
foreground = "Black";
} else if((foreground == "Black") && (App.Current.RequestedTheme == ApplicationTheme.Dark))
{
foreground = "White";
}
if (foreground != "Default")
{
//If the color is set to "Default" we don't need to set the color
var color = (Color)XamlBindingHelper.ConvertValue(typeof(Color), foreground);
var brush = new SolidColorBrush(color);
symbolIcon.Foreground = brush;
}
navigationViewItem.Icon = symbolIcon;
return navigationViewItem;
}
public static NavigationViewItem GenerateCategoryNavigationViewItemFromXML(string category, string icon)
{
return GenerateCategoryNavigationViewItemFromXML(category, icon, null);
}
public static Button GenerateExpanderButton(string text, string name, Orientation orientation, Symbol icon, Thickness iconMargin, Thickness btnMargin)
{
Button btn = new Button();
btn.Name = name + text;
btn.Margin = btnMargin;
StackPanel ButtonStackPanel = new StackPanel();
ButtonStackPanel.Orientation = orientation;
SymbolIcon ButtonSymbolIcon = new SymbolIcon();
ButtonSymbolIcon.Symbol = icon;
ButtonSymbolIcon.Margin = iconMargin;
TextBlock ButtonTextBlock = new TextBlock();
ButtonTextBlock.Text = text;
ButtonStackPanel.Children.Add(ButtonSymbolIcon);
ButtonStackPanel.Children.Add(ButtonTextBlock);
btn.Content = ButtonStackPanel;
return btn;
}
public static Expander GenerateExpanderFromXML(string name, string description, string path, string psVersion, string icon, string category, string inputType)
{
//Build the base Expander
Expander tmp = new Microsoft.UI.Xaml.Controls.Expander();
tmp.Name = name;
tmp.IsExpanded = false;
tmp.ExpandDirection = Microsoft.UI.Xaml.Controls.ExpandDirection.Down;
tmp.HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Stretch;
tmp.Padding = new Microsoft.UI.Xaml.Thickness(20);
tmp.Margin = new Microsoft.UI.Xaml.Thickness(25, 10, 25, 0);
//Header Stack Panel
StackPanel headerStack = new StackPanel();
headerStack.Orientation = Microsoft.UI.Xaml.Controls.Orientation.Horizontal;
headerStack.Padding = new Microsoft.UI.Xaml.Thickness(20);
//Header Icon
SymbolIcon headerIcon = new SymbolIcon();
headerIcon.Symbol = (Symbol)System.Enum.Parse(typeof(Symbol), icon);
headerIcon.Margin = new Microsoft.UI.Xaml.Thickness(0, 0, 10, 0);
headerStack.Children.Add(headerIcon);
//Header Title
TextBlock headerTextBlock = new TextBlock();
headerTextBlock.Text = name;
headerStack.Children.Add(headerTextBlock);
//Add Header Stack Panel to the Expander
tmp.Header = headerStack;
//Content Stack Panel
StackPanel headerContentStackPanel = new StackPanel();
//Description
TextBlock headerContentTextBlock = new TextBlock();
headerContentTextBlock.Text = "Description: " + description;
headerContentTextBlock.HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Left;
headerContentTextBlock.Margin = new Microsoft.UI.Xaml.Thickness(5);
headerContentStackPanel.Children.Add(headerContentTextBlock);
//Explore and Run Button Stack Panel
StackPanel headerContentStackPanelStackPanel = new StackPanel();
headerContentStackPanelStackPanel.Orientation = Microsoft.UI.Xaml.Controls.Orientation.Horizontal;
headerContentStackPanelStackPanel.HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Right;
//Explore Button
Button headerContentExploreButton = GenerateExpanderButton("Explore", name, Microsoft.UI.Xaml.Controls.Orientation.Horizontal, Symbol.Globe, new Microsoft.UI.Xaml.Thickness(0, 0, 5, 0), new Microsoft.UI.Xaml.Thickness(0, 0, 5, 0));
//Run Button
Button headerContentRunButton = GenerateExpanderButton("Run", name, Microsoft.UI.Xaml.Controls.Orientation.Horizontal, Symbol.Play, new Microsoft.UI.Xaml.Thickness(0, 0, 5, 0), new Microsoft.UI.Xaml.Thickness(0));
//PS Version and Input Type
StackPanel MetadataIcons = new StackPanel {Orientation = Microsoft.UI.Xaml.Controls.Orientation.Horizontal};
Image PSImage = new Image();
PSImage.Margin = new Thickness(5,0,5,0);
if(psVersion == "PS5")
{
void Image_Loaded(object sender, RoutedEventArgs e)
{
Image img = sender as Image;
BitmapImage bitmapImage = new BitmapImage();
img.Width = bitmapImage.DecodePixelWidth = 20;
bitmapImage.UriSource = new Uri(img.BaseUri, "Assets/Powershell5.png");
img.Source = bitmapImage;
}
PSImage.Loaded += Image_Loaded;
ToolTip toolTip = new ToolTip();
toolTip.Content = "Runs with Windows PowerShell 5";
ToolTipService.SetToolTip(PSImage, toolTip);
MetadataIcons.Children.Add(PSImage);
}
else if(psVersion == "PS7")
{
void Image_Loaded(object sender, RoutedEventArgs e)
{
Image img = sender as Image;
BitmapImage bitmapImage = new BitmapImage();
img.Width = bitmapImage.DecodePixelWidth = 20;
bitmapImage.UriSource = new Uri(img.BaseUri, "Assets/Powershell7.ico");
img.Source = bitmapImage;
}
PSImage.Loaded += Image_Loaded;
ToolTip toolTip = new ToolTip();
toolTip.Content = "Runs with PowerShell 7";
ToolTipService.SetToolTip(PSImage, toolTip);
MetadataIcons.Children.Add(PSImage);
}
if (inputType == "Machine")
{
SymbolIcon InputTypeIcon = new SymbolIcon();
InputTypeIcon.Symbol = Symbol.GoToStart;
InputTypeIcon.Width = 20;
InputTypeIcon.Height = 20;
ToolTip toolTip = new ToolTip();
toolTip.Content = "Requires machine input to run";
ToolTipService.SetToolTip(InputTypeIcon, toolTip);
MetadataIcons.Children.Add(InputTypeIcon);
}
//Add Buttons to Buttons Stack Panel
if(SettingExplorer == "true" || SettingExplorer == "True")
{
headerContentStackPanelStackPanel.Children.Add(headerContentExploreButton);
}
headerContentStackPanelStackPanel.Children.Add(headerContentRunButton);
//Add Buttons Stack Panel to Parent Content Stack Panel
headerContentStackPanel.Children.Add(headerContentStackPanelStackPanel);
//Add PS Version to Parent Content Stack Panel
headerContentStackPanel.Children.Add(MetadataIcons);
//Finalize the Expander UI content
tmp.Content = headerContentStackPanel;
//Add Run and Explore click events
void RunButtonMethods()
{
LaunchScript(path, psVersion, inputType);
WriteReportingRecord(name, path, CurrentInput, psVersion);
}
headerContentRunButton.Click += (sender, e) => RunButtonMethods();
headerContentExploreButton.Click += (sender, e) => LaunchExplorer(path);
return tmp;
}
public static XDocument guiConfig;
public static string SettingExplorer;
public static string SettingReportingLogLocation;
WindowsSystemDispatcherQueueHelper m_wsdqHelper; // See below for implementation.
MicaController m_backdropController;
SystemBackdropConfiguration m_configurationSource;
bool TrySetSystemBackdrop()
{
if (Microsoft.UI.Composition.SystemBackdrops.MicaController.IsSupported())
{
m_wsdqHelper = new WindowsSystemDispatcherQueueHelper();
m_wsdqHelper.EnsureWindowsSystemDispatcherQueueController();
// Create the policy object.
m_configurationSource = new SystemBackdropConfiguration();
this.Activated += Window_Activated;
this.Closed += Window_Closed;
((FrameworkElement)this.Content).ActualThemeChanged += Window_ThemeChanged;
// Initial configuration state.
m_configurationSource.IsInputActive = true;
SetConfigurationSourceTheme();
m_backdropController = new Microsoft.UI.Composition.SystemBackdrops.MicaController();
// Enable the system backdrop.
// Note: Be sure to have "using WinRT;" to support the Window.As<...>() call.
m_backdropController.AddSystemBackdropTarget(this.As<Microsoft.UI.Composition.ICompositionSupportsSystemBackdrop>());
m_backdropController.SetSystemBackdropConfiguration(m_configurationSource);
return true; // succeeded
}
return false; // Mica is not supported on this system
}
class WindowsSystemDispatcherQueueHelper
{
[StructLayout(LayoutKind.Sequential)]
struct DispatcherQueueOptions
{
internal int dwSize;
internal int threadType;
internal int apartmentType;
}
[DllImport("CoreMessaging.dll")]
private static extern int CreateDispatcherQueueController([In] DispatcherQueueOptions options, [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object dispatcherQueueController);
object m_dispatcherQueueController = null;
public void EnsureWindowsSystemDispatcherQueueController()
{
if (Windows.System.DispatcherQueue.GetForCurrentThread() != null)
{
// one already exists, so we'll just use it.
return;
}
if (m_dispatcherQueueController == null)
{
DispatcherQueueOptions options;
options.dwSize = Marshal.SizeOf(typeof(DispatcherQueueOptions));
options.threadType = 2; // DQTYPE_THREAD_CURRENT
options.apartmentType = 2; // DQTAT_COM_STA
CreateDispatcherQueueController(options, ref m_dispatcherQueueController);
}
}
}
private void Window_Activated(object sender, Microsoft.UI.Xaml.WindowActivatedEventArgs args)
{
m_configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated;
}
private void Window_Closed(object sender, WindowEventArgs args)
{
// Make sure any Mica/Acrylic controller is disposed
// so it doesn't try to use this closed window.
if (m_backdropController != null)
{
m_backdropController.Dispose();
m_backdropController = null;
}
this.Activated -= Window_Activated;
m_configurationSource = null;
}
private void Window_ThemeChanged(FrameworkElement sender, object args)
{
if (m_configurationSource != null)
{
SetConfigurationSourceTheme();
}
}
private void SetConfigurationSourceTheme()
{
switch (((FrameworkElement)this.Content).ActualTheme)
{
case ElementTheme.Dark: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Dark; break;
case ElementTheme.Light: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Light; break;
case ElementTheme.Default: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Default; break;
}
}
public MainWindow()
{
this.InitializeComponent();
TrySetSystemBackdrop();
//Title Bar customization
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
//Set the Navigation Properties
XDocument settingsXML = XDocument.Load(@"Settings.xml");
foreach (XElement item in from y in settingsXML.Descendants("Item") select y)
{
if (item.Attribute("Name").Value == "SettingApplicationTitle")
{
var AppTitle = item.Attribute("Setting").Value.ToString();
AppTitleTextBlock.Text = AppTitle;
MainTitle = AppTitle;
}
if(item.Attribute("Name").Value == "SettingApplicationSubTitle")
{
MainSubTitle = item.Attribute("Setting").Value.ToString();
}
if (item.Attribute("Name").Value == "SettingShowExplorer")
{
SettingExplorer = item.Attribute("Setting").Value.ToString();
}
if (item.Attribute("Name").Value == "SettingReportingLogLocation")
{
SettingReportingLogLocation = item.Attribute("Setting").Value.ToString();
if (SettingReportingLogLocation.Contains("@AppPath"))
{
SettingReportingLogLocation = SettingReportingLogLocation.Replace("@AppPath", Environment.CurrentDirectory);
}
}
}
//Load categories into the UI
XDocument categoryConfig = XDocument.Load(@"XML\Categories.xml");
guiConfig = XDocument.Load(@"XML\Scripts.xml");
foreach (XElement item in from y in categoryConfig.Descendants("Item") select y)
{
Pages.Add(GenerateCategoryPageFromXML(item.Attribute("category").Value)); //Add the page to a collection for later use
NavigationViewItem currentNavItem = GenerateCategoryNavigationViewItemFromXML(item.Attribute("category").Value, item.Attribute("icon").Value, item.Attribute("foreground").Value);
NavigationViews.Add(currentNavItem);
MainNav.MenuItems.Add(currentNavItem);
}
//Select the first navigation item
//TODO: Should this be a setting?
MainNav.SelectedItem = MainNav.MenuItems[0]; //Index 0 is dashboard
//We've got to check for updates in the main window due to a WinUI limitation that currently exists
CheckForUpdates();
}
private async void CheckForUpdates()
{
//Check for updates if enabled
XDocument settingsXML = XDocument.Load(@"Settings.xml");
foreach (XElement item in from y in settingsXML.Descendants("Item") select y)
{
if (item.Attribute("Name").Value == "SettingAutomaticUpdates")
{
if (item.Attribute("Setting").Value == "true" || item.Attribute("Setting").Value == "True")
{
var updateRequired = false;
LoadingText.Text = "Checking for updates...";
XDocument updateXML = XDocument.Load("https://raw.githubusercontent.com/nkasco/IT-Admin-Toolkit-WinUI/master/UpdateInfo.xml");
XElement updateXMLData = updateXML.Descendants("Item").Last();
var updateVersion = updateXMLData.Attribute("version").Value;
Version updateVersionv = new Version(updateVersion);
var currentVersion = CurrentVersion();
Version currentVersionv = new Version(currentVersion);
if (currentVersionv < updateVersionv)
{
updateRequired = true;
}
if (updateRequired == true)
{
LoadingText.Text = "Downloading update...";
var scriptName = "Updater.ps1";
var scriptPath = Environment.CurrentDirectory + "\\" + scriptName;
LaunchScript(scriptPath, " -InstallPath \"" + Environment.CurrentDirectory + "\" -DownloadURL \"" + updateXMLData.Attribute("link").Value + "\"", "PS5", "None", "false", "true", "true");
await Task.Run(() => Task.Delay(1000000)); //TODO: This needs fixed, for some reason WaitForExit() isn't working
//TODO: Run LoadingPhrase() on a loop
}
}
}
}
LoadingStack.Visibility = Visibility.Collapsed;
MainNav.Visibility = Visibility.Visible;
}
private string CurrentVersion()
{
XDocument changelogDetail = XDocument.Load(@"Changelog.xml");
var version = changelogDetail.Descendants("Item").Last().Attribute("version").Value;
return version;
}
private string LoadingPhrase()
{
//Return a random string for the loading window
var phrase = "";
return phrase;
}
//TODO: Terminal output WIP
public static void myProgressEventHandler(object sender, DataAddedEventArgs e)
{
InformationRecord newRecord = ((PSDataCollection<InformationRecord>)sender)[e.Index];
MyText += newRecord.ToString();
}
private static string myText;
public static string MyText
{
get => myText;
set => myText = value;
}
private void UpdateMainWindowBindings()
{
//Bindings.Update();
}
public void updateMainWindowBindings()
{
UpdateMainWindowBindings();
}
//End WIP
private void MainNav_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
//TODO: Feels like there may be a small memory leak with Navigate(), this should be addressed
if (args.SelectedItemContainer.Content.ToString() == "Settings")
{
//Show Settings page
contentFrame.Navigate(SettingsPage);
ContentSplitView.IsPaneOpen = false;
MachineDetailsToggleButton.IsChecked = false;
MachineInputs.Visibility = Visibility.Collapsed;
} else if (args.SelectedItemContainer.Content.ToString() == "Reporting")
{
//Show Reporting page
contentFrame.Navigate(ReportingPage);
ContentSplitView.IsPaneOpen = false;
MachineDetailsToggleButton.IsChecked = false;
MachineInputs.Visibility = Visibility.Collapsed;
}
else if (args.SelectedItemContainer.Content.ToString() == "All Scripts")
{
//TODO: Show all scripts in button form here
}
else if (args.SelectedItemContainer.Content.ToString() == "Dashboard")
{
//Show Dashboard page
contentFrame.Navigate(DashboardPage);
ContentSplitView.IsPaneOpen = false;
MachineDetailsToggleButton.IsChecked = false;
MachineInputs.Visibility = Visibility.Collapsed;
}
else
{
//Show the selected page
var PageName = args.SelectedItemContainer.Content.ToString();
var _page = from Page in Pages
where Page.Name == PageName
select Page;
contentFrame.Content = _page.FirstOrDefault();
MachineInputs.Visibility = Visibility.Visible;
}
}
private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}
private void MachineDetailsToggleButton_Click(object sender, RoutedEventArgs e)
{
if (ContentSplitView.IsPaneOpen == true)
{
ContentSplitView.IsPaneOpen = false;
} else
{
ContentSplitView.IsPaneOpen= true;
}
}
private void MachineMultipleToggleButton_Click(object sender, RoutedEventArgs e)
{
if(MachineComboBox.Visibility == Visibility.Visible)
{
SingleOrMulti = "Multi";
PingSymbol.Visibility = Visibility.Collapsed;
MachineComboBox.Visibility = Visibility.Collapsed;
MultipleMachineInput.Visibility = Visibility.Visible;
MachineMultipleClearButton.Visibility = Visibility.Visible;
//Ensure the information pane is hidden
ContentSplitView.IsPaneOpen = false;
MachineDetailsToggleButton.IsChecked = false;
MachineDetailsToggleButton.Visibility = Visibility.Collapsed;
} else
{
SingleOrMulti = "Single";
MachineComboBox.Visibility = Visibility.Visible;
MultipleMachineInput.Visibility = Visibility.Collapsed;
MachineMultipleClearButton.Visibility = Visibility.Collapsed;
//Show the information pane button
ContentSplitView.Visibility = Visibility.Visible;
MachineDetailsToggleButton.Visibility = Visibility.Visible;
}
}
private void MachineMultipleClearButton_Click(object sender, RoutedEventArgs e)
{
MultipleMachineInput.Text = null;
}
private void HideScriptTerminal_Click(object sender, RoutedEventArgs e)
{
//This probably needs to be cleaned up a bit
if (HideScriptTerminal.IsChecked == false && TerminalRowConfig.Height != GridLength.Auto)
{
ContentFrameRowConfig.Height = new GridLength(1, GridUnitType.Star);
TerminalRowConfig.Height = GridLength.Auto;
ContentFrameScrollViewer.Visibility = Visibility.Visible;
}
if(TerminalRow.Visibility == Visibility.Visible)
{
ContentFrameRowConfig.Height = new GridLength(1, GridUnitType.Star);
TerminalRow.Visibility = Visibility.Collapsed;
} else if (ContentFrameScrollViewer.Visibility == Visibility.Collapsed) {
ContentFrameRowConfig.Height = GridLength.Auto;
TerminalRowConfig.Height = new GridLength(1, GridUnitType.Star);
TerminalRow.Visibility = Visibility.Visible;
}
else
{
ContentFrameRowConfig.Height = new GridLength(1, GridUnitType.Star);
TerminalRow.Visibility = Visibility.Visible;
}
}
private void FullScreenTerminal_Click(object sender, RoutedEventArgs e)
{
if(ContentFrameScrollViewer.Visibility == Visibility.Visible)
{
//Full Screen
ContentFrameScrollViewer.Visibility = Visibility.Collapsed;
ContentFrameRowConfig.Height = GridLength.Auto;
ScriptTerminal.Height = 540; //This needs to be more dynamic to fill the space
TerminalRowConfig.Height = new GridLength(1, GridUnitType.Star);
} else
{
//Docked
ContentFrameRowConfig.Height = new GridLength(1, GridUnitType.Star);
ContentFrameScrollViewer.Visibility = Visibility.Visible;
ScriptTerminal.Height = 150;
TerminalRowConfig.Height = GridLength.Auto;
}
}
public static void WriteReportingRecord(string ScriptTitle, string ScriptFilePath, string MachineInput, string Type)
{
//Name, Input, Time, User Executing, Host Executing
var newRecord = new List<ReportingRecord>
{
new ReportingRecord {
ScriptName = ScriptTitle,
ScriptPath = ScriptFilePath,
HostExecuting = Environment.MachineName,
FileExtension = Type,
Target = MachineInput,
UserExecuting = Environment.UserName,
DateTime = DateTime.Now
}
};
var records = GetReportingRecords();
var reportFilePath = SettingReportingLogLocation;
using var writer = new StreamWriter(reportFilePath);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteHeader<ReportingRecord>();
csv.NextRecord();
foreach (var record in records)
{
csv.WriteRecord(record);
csv.NextRecord();
}
csv.WriteRecords(newRecord);
csv.NextRecord();
}
public static List<ReportingRecord> GetReportingRecords()
{
var reportFilePath = SettingReportingLogLocation;
using var reader = new StreamReader(reportFilePath);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
var records = csv.GetRecords<ReportingRecord>();
return records.ToList();
}
private void ClearTerminal_Click(object sender, RoutedEventArgs e)
{
//ScriptTerminal.Text = "";
MyText = "";
//Bindings.Update();
}
private void CopyTerminal_Click(object sender, RoutedEventArgs e)
{
ScriptTerminal.SelectAll();
ScriptTerminal.CopySelectionToClipboard();
}
private void MultipleMachineInput_TextChanged(object sender, TextChangedEventArgs e)
{
//Replace new lines with commas
CurrentMultipleInput = MultipleMachineInput.Text.Trim().Replace("\r", ",");
}
private PSDataCollection<PSObject> LoadMachineInfo(string Machine)
{
InitialSessionState _initialSessionState = InitialSessionState.CreateDefault2();
_initialSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
var script = AppContext.BaseDirectory + @"\MachineInfoGather.ps1";
//var script = @"C:\scripts\MachineInfo.ps1";
using var run = RunspaceFactory.CreateRunspace(_initialSessionState);
run.Open();
var ps = PowerShell.Create(run);
ps.AddCommand("Import-Module");
ps.AddParameter("SkipEditionCheck");
ps.AddArgument("CIMcmdlets");
ps.Invoke();
var err = run.SessionStateProxy.PSVariable.GetValue("error");
System.Diagnostics.Debug.WriteLine(err);//This will reveal any error loading
ps.AddCommand(script);
ps.AddArgument(Machine);
IAsyncResult ab = ps.BeginInvoke();
PSDataCollection<PSObject> result = ps.EndInvoke(ab);
run.Close();
return result;
}
private async void MachineComboBox_TextChangedAsync(object sender, TextChangedEventArgs e)
{
async Task<bool> UserKeepsTyping()
{
var txt = MachineComboBox.Text; // remember text
await Task.Delay(1000); // wait some
return txt != MachineComboBox.Text; // return that text chaged or not
}
if (await UserKeepsTyping()) return;
if (MachineComboBox.Text != "")
{
//Set public variable with the current input
CurrentInput = MachineComboBox.Text;
PSDataCollection<PSObject> RunPing(string CurrentInput)
{
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddCommand("Test-Connection")