forked from bcdady/MyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJEAToolkitHelper.ps1
875 lines (797 loc) · 49.7 KB
/
JEAToolkitHelper.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
########################################################################################
# JEA Toolkit Helper
# Version 1.0
# by the Windows Server and System Center CAT team - http://aka.ms/bcb
# Please send feedback to [email protected]
########################################################################################
param (
[String]$SMAEndpointWS = "",
[String]$SMAEndpointPort = "9090"
)
$ToolVersion = "1.0"
$Global:DefaultSDDL = "O:NSG:BAD:P(A;;GA;;;BA)(A;;GA;;;RM)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)"
################################################
# Functions
################################################
function Popup()
{
param (
[String]$Message
)
$a = new-object -comobject wscript.shell
$b = $a.popup($Message,0,"JEA Toolkit Helper",0)
}
Function AddArray()
{
param (
[String]$Module,
[String]$Name,
[String]$Parameter,
[String]$ValidateSet = "",
[String]$ValidatePattern = ""
)
$Global:CommandArray = @()
$tmpObject = select-object -inputobject "" IsChecked, Module, Name, Parameter, ValidateSet, ValidatePattern
$tmpObject.Ischecked = $false
$tmpObject.Module = $Module
$tmpObject.Name = $Name
$tmpObject.Parameter = $Parameter
$tmpObject.ValidateSet = $ValidateSet
$tmpObject.ValidatePattern = $ValidatePattern
$Global:CommandArray += $tmpObject
If ($FORM.FindName('CSVGrid').Items.Count -eq 0)
{$FORM.FindName('CSVGrid').ItemsSource = $Global:CommandArray}
else {$FORM.FindName('CSVGrid').ItemsSource += $Global:CommandArray}
}
Function UpdateDelegation()
{
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Validating account list to generate SDDL string in script output..."
$Global:SDDLAccountList = @()
$Accounts = $FORM.FindName('ConfigureAllowedUsersTextBox').Text.Split(";")
$Global:strSDDL = "O:NSG:BAD:P"
$ErrorAccounts = 0
foreach ($Account in $Accounts)
{
$objUser = New-Object System.Security.Principal.NTAccount($Account)
$eap = $ErrorActionPreference = "SilentlyContinue"
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
if (!$?) {
$ErrorActionPreference =$eap
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : SID could not be resolved for account $Account. This account has not been added to the resulting SDDL output."
$ErrorAccounts += 1
}
else
{
$Global:strSDDL = $Global:strSDDL + "(A;;GA;;;" + $strSID + ")"
$Global:SDDLAccountList +=$Account
}
}
$Global:strSDDL = $Global:strSDDL + "S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)"
If ($ErrorAccounts -eq $Accounts.Count)
{
$Global:strSDDL = $Global:DefaultSDDL
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : None of the user(s) or group(s) provided could be resolved, so the JEA Toolkit will use the default security."
}
UpdateScriptOutput
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Validating account list to generate SDDL string in script output...done!"
}
Function UpdateCmdletList()
{
$FORM.FindName('PickCmdletComboBox').Items.Clear()
Foreach ($CmdletItem in $Global:CmdletList)
{ If ($CmdletItem.Name.Length -gt 3) {$FORM.FindName('PickCmdletComboBox').Items.Add($CmdletItem.Name) | out-null} }
}
Function UpdateModuleList()
{
$FORM.FindName('FilterModuleComboBox').Items.Clear()
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Loading modules list...This may take a few seconds, please wait..."
#$ModuleList = Get-Module | Sort-Object Name | Select Name
$ModuleList = Get-Module -ListAvailable | Sort-Object Name | Select Name
Foreach ($ModuleItem in $ModuleList)
{ If ($ModuleList.Name.Length -gt 3) {$FORM.FindName('FilterModuleComboBox').Items.Add($ModuleItem.Name) | out-null} }
}
Function UpdateScriptOutput()
{
If ($FORM.FindName('CSVGrid').ItemsSource.Count -gt 0)
{
$OriginalScript = @"
configuration TOOLKITNAMEPLACEHOLDER
{
Import-DscResource -module xjea
xJeaToolKit TOOLKITNAMEPLACEHOLDER
{
Name = 'TOOLKITNAMEPLACEHOLDER'
CommandSpecs = @"
Module, Name,Parameter,ValidateSet,ValidatePattern
COMMANDSPLACEHOLDER
"`@
# Or you can use this command, replacing it with the right file and path names
# CommandSpecs = Get-Content “C:\AuditorToolkit\Toolkit.csv” –Delimiter “NoSuch”
}
xJeaEndPoint TOOLKITNAMEPLACEHOLDEREP
{
Name = 'TOOLKITNAMEPLACEHOLDEREP'
Toolkit = 'TOOLKITNAMEPLACEHOLDER'
SecurityDescriptorSddl = 'SDDLSTRINGPLACEHOLDER'
DependsOn = '[xJeaToolKit]TOOLKITNAMEPLACEHOLDER'
#CleanAll = 'True'
}
}
SDDLDESCRIPTIONPLACEHOLDER
#The first two lines of the script below are enabled when importing the toolkit,
#and would have to be enabled in case of manual import as well
#Note that these same lines are also used for removal
#(in which case the script enabled the 'CleanAll' property in the EndPoint configuration)
#The WinRM service restart is not done by the script, but you could chose to add for manual execution
#TOOLKITNAMEPLACEHOLDER -OutputPath C:\DSCDemo
#Start-DscConfiguration -Path C:\DSCDemo -ComputerName localhost -Verbose -wait -debug
#start-sleep -Seconds 30
#The next three lines are the only ones executed when testing a toolkit on the local computer.
#This is also what you would run, should you want to do this manually
#`$s = New-PSSession -ComputerName . -ConfigurationName TOOLKITNAMEPLACEHOLDEREP
#Invoke-command `$s {get-command} |out-string
#Remove-PSSession `$s
#Alternatively, once a new session has been created, this is how you could enter
#and work in the JEA session before exiting and removing it.
#Enter-pssession `$s
#Exit-PSSession
"@
$NewScriptContent = ""
Foreach ($CurrentRow in $FORM.FindName('CSVGrid').ItemsSource)
{
If ($CurrentRow.Module -eq $null)
{$NewScriptContent+= "," + $CurrentRow.Name.Trim() + "," + $CurrentRow.Parameter + "," + $CurrentRow.ValidateSet + "," + $CurrentRow.ValidatePattern + "`r`n"}
else
{$NewScriptContent+= $CurrentRow.Module.Trim() + "," + $CurrentRow.Name.Trim() + "," + $CurrentRow.Parameter + "," + $CurrentRow.ValidateSet + "," + $CurrentRow.ValidatePattern + "`r`n"}
}
$FORM.FindName('ScriptOutputTextBlock').Text=$OriginalScript.Replace("COMMANDSPLACEHOLDER", $NewScriptContent)
$FORM.FindName('ScriptOutputTextBlock').Text=$FORM.FindName('ScriptOutputTextBlock').Text.Replace("TOOLKITNAMEPLACEHOLDER", $FORM.FindName('ToolkitNameTextBox').Text)
$FORM.FindName('ScriptOutputTextBlock').Text=$FORM.FindName('ScriptOutputTextBlock').Text.Replace("SDDLSTRINGPLACEHOLDER", $Global:strSDDL)
If ($Global:strSDDL -eq $Global:DefaultSDDL)
{
$FORM.FindName('ScriptOutputTextBlock').Text=$FORM.FindName('ScriptOutputTextBlock').Text.Replace("SDDLDESCRIPTIONPLACEHOLDER", "#SDDL Description : Default SDDL (BULTIN\Administrators group on the target endpoint)")
}
else
{
$SDDLDescriptionOutput = "#SDDL Description :"
foreach ($SDDLAccount in $Global:SDDLAccountList) {$SDDLDescriptionOutput = $SDDLDescriptionOutput + $SDDLAccount + ";"}
$FORM.FindName('ScriptOutputTextBlock').Text=$FORM.FindName('ScriptOutputTextBlock').Text.Replace("SDDLDESCRIPTIONPLACEHOLDER", $SDDLDescriptionOutput)
}
$FORM.FindName('ImportToolkit').IsEnabled = $true
$FORM.FindName('TestToolkit').IsEnabled = $true
$FORM.FindName('RemoveToolkit').IsEnabled = $true
$FORM.FindName('BypassSMACmdletsNotPresentCB').IsEnabled = $true
$FORM.FindName('BypassSMACmdletsNotPresentLabel').IsEnabled = $true
}
else
{
$FORM.FindName('ScriptOutputTextBlock').Text="Script output will be updated here"
$FORM.FindName('ImportToolkit').IsEnabled = $false
$FORM.FindName('TestToolkit').IsEnabled = $false
$FORM.FindName('RemoveToolkit').IsEnabled = $false
$FORM.FindName('BypassSMACmdletsNotPresentCB').IsEnabled = $false
$FORM.FindName('BypassSMACmdletsNotPresentLabel').IsEnabled = $false
}
}
################################################
# Form definition
################################################
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[XML]$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="NoResize"
Title="JEA Toolkit Helper" Height="695" Width="840">
<Window.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border" Background="LightBlue" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="25,25,0,0" >
<ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="LightBlue" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TabControl>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Design Helper" Margin="2,0,0,0" VerticalAlignment="Center" />
</StackPanel>
</TabItem.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="45"/>
<RowDefinition Height="45"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="320"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Label FontWeight="Bold" Content="We are working with Toolkit named " HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0" Grid.Row="0"></Label>
<TextBox Text="DemoXYZ" Name="ToolkitNameTextBox" IsEnabled="True" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="230,0,0,0" Width="100"></TextBox>
<Label Content="You can import an existing CSV file" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0" Grid.Row="1"></Label>
<Button Content="Import CSV File..." Name="ImportCSVFile" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="230,0,0,0" Width="120" Grid.Row="1"/>
<ComboBox Name="ImportCSVFileAction" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="380,0,0,0" Width="150" Grid.Row="1"></ComboBox>
<CheckBox Name="ImportXMLCB" IsChecked="True" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="550,0,0,0" Grid.Row="1"></CheckBox>
<Label Name="ImportXMLLabel" Content="Import delegation data as well, if available" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="570,0,0,0" Grid.Row="1"></Label>
<Label HorizontalAlignment="Left" Margin="5,0,0,0" MaxWidth="170" Grid.Row="2">
<TextBlock Name="PickCmdletLabel" Text="Or you can pick a cmdlet and - optionally - properties" TextWrapping= "Wrap"></TextBlock>
</Label>
<ComboBox Name="PickCmdletComboBox" IsEditable="False" HorizontalAlignment="Left" VerticalAlignment="Center" Width="215" Margin="230,0,0,0" Grid.Row="2"></ComboBox>
<ComboBox Name="PickPropertiesComboBox" HorizontalAlignment="Left" VerticalAlignment="Center" Width="215" Margin="475,0,0,0" Grid.Row="2">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Margin="5" IsChecked="{Binding PropertyChecked}"/>
<TextBlock Margin="5" Text="{Binding PropertyName}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="Add to Toolkit" Name="AddToGrid" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="710,0,0,0" Width="100" Grid.Row="2"/>
<Label HorizontalAlignment="Left" Margin="5,0,0,0" MaxWidth="210" Grid.Row="3" Grid.RowSpan="2">
<TextBlock Name="PickModuleLabel" Text="Or you can add a full/partial module, or use it to filter the cmdlets list" TextWrapping= "Wrap"></TextBlock>
</Label>
<ComboBox Name="FilterModuleComboBox" IsEditable="False" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120" Margin="230,0,0,0" Grid.Row="3"></ComboBox>
<Button Content="Add to Toolkit" Name="AddModuleFullToGridButton" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="365,0,0,0" Width="100" Grid.Row="3"/>
<Button Content="Add Get-* only" Name="AddModuleGetToGridButton" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="480,0,0,0" Width="100" Grid.Row="3"/>
<Button Content="Filter Cmdlets" Name="FilterModuleButton" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="595,0,0,0" Width="100" Grid.Row="3"/>
<Button Content="Remove Filter" Name="RemoveFilterModuleButton" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="710,0,0,0" Width="100" Grid.Row="3"/>
<Label Content="Module to import" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="230,0,0,0" Grid.Row="4"></Label>
<TextBox Text="" Name="ImportModuleTextBox" HorizontalAlignment="Left" VerticalAlignment="Center" Width="215" Margin="365,0,0,0" Grid.Row="4"></TextBox>
<Button Content="Import Module" Name="ImportModuleButton" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="595,0,0,0" Width="100" Grid.Row="4"/>
<Label Name="PickRunbookLabelContainer" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5,0,0,0" MaxWidth="230" Grid.Row="5">
<TextBlock Name="PickRunbookLabel" Text="Or you can pick SMA Runbook(s)" TextWrapping= "Wrap"></TextBlock>
</Label>
<ComboBox Name="PickRunbooksComboBox" HorizontalAlignment="Left" VerticalAlignment="Center" Width="460" Margin="230,0,0,0" Grid.Row="5">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Margin="5" IsChecked="{Binding RunbookChecked}"/>
<TextBlock Margin="5" Text="{Binding RunbookName}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="Add to Toolkit" Name="AddRunbookToGrid" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="710,0,0,0" Width="100" Grid.Row="5"/>
<DataGrid AutoGenerateColumns="False" Margin="10,0,0,0" Name="CSVGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Height="320" Width="800" ItemsSource="{Binding}" SelectionUnit="Cell" Grid.Row="6">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsChecked}"/>
<DataGridTextColumn Binding="{Binding Path=Module}" Header="Module"/>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Path=Parameter}" Header="Parameter" />
<DataGridTextColumn Binding="{Binding Path=ValidateSet}">
<DataGridTextColumn.Header>
<TextBlock Text="ValidateSet" ToolTipService.ToolTip="Semi-colon separated list of allowed parameters. An empty list means that all parameters are allowed." />
</DataGridTextColumn.Header>
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="ToolTip" Value="{Binding Description}" />
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=ValidatePattern}">
<DataGridTextColumn.Header>
<TextBlock Text="ValidatePattern" ToolTipService.ToolTip="A regular expression. This is an optional parameter. See examples here : http://technet.microsoft.com/en-us/library/hh847880.aspx" />
</DataGridTextColumn.Header>
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="ToolTip" Value="{Binding Description}" />
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Add Row" Name="AddRowGrid" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" Width="150" Grid.Row="7"/>
<Button Content="Remove Selected Row(s)" Name="DeleteRowGrid" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="170,0,0,0" Width="150" Grid.Row="7"/>
<Button Content="Remove All Rows" Name="DeleteAllRows" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="330,0,0,0" Width="150" Grid.Row="7"/>
<CheckBox Name="ConfigureAllowedUsersCB" IsChecked="False" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" Grid.Row="8"></CheckBox>
<Label Name="ConfigureAllowedUsersLabel" Content="Configure Allowed Users (default is BUILTIN\Administrators)" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="30,0,0,0" Grid.Row="8"></Label>
<TextBox Text="" Name="ConfigureAllowedUsersTextBox" IsEnabled="False" Grid.Row="8" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="370,0,0,0" Width="280"></TextBox>
<Button Content="Update Delegation" Name="UpdateDelegation" IsEnabled="False" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="660,0,0,0" Width="150" Grid.Row="8"/>
</Grid>
</TabItem>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Script output" Margin="2,0,0,0" VerticalAlignment="Center" />
</StackPanel>
</TabItem.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="480"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBox Name="ScriptOutputTextBlock" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Grid.Row="0">
Script output will be updated here
</TextBox>
<Label Content="You can copy and paste this script in PowerShell/ISE, or use the button on the right to copy it to the clipboard" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" Width="630" Grid.Row="1"></Label>
<Button Content="Copy to Clipboard" Name="CopyToClipboard" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="660,0,0,0" Width="150" Grid.Row="1"/>
<Label Content="You can also save this output as a CSV File Name" Name="CSVFileNameLabel" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" Grid.Row="2"></Label>
<TextBox Text="DemoXYZ.csv" Name="CSVFileNameTextBox" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="300,0,0,0" Width="150" Grid.Row="2"></TextBox>
<CheckBox Name="ExportXMLCB" IsChecked="True" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="460,0,0,0" Grid.Row="2"></CheckBox>
<Label Name="ExportXMLLabel" Content="Export delegation data as well" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="480,0,0,0" Grid.Row="2"></Label>
<Button Content="Export to CSV" Name="ExportCSVFile" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="660,0,0,0" Width="150" Grid.Row="2"/>
<Label Content="Finally, you can import, test or this configuration on the local machine" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" Width="630" Grid.Row="3"></Label>
<Button Content="Remove" Name="RemoveToolkit" IsEnabled="False" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="700,0,0,0" Width="100" Grid.Row="3"/>
<Button Content="Test" Name="TestToolkit" IsEnabled="False" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="550,0,0,0" Width="100" Grid.Row="3"/>
<Button Content="Import" Name="ImportToolkit" IsEnabled="False" Visibility="visible" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="400,0,0,0" Width="100" Grid.Row="3"/>
<CheckBox Name="BypassSMACmdletsNotPresentCB" IsEnabled="False" IsChecked="True" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="400,0,0,0" Grid.Row="4"></CheckBox>
<Label Name="BypassSMACmdletsNotPresentLabel" IsEnabled="False" Content="If SMA module not present, disable SMA cmdlets before toolkit import" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="420,0,0,0" Grid.Row="4"></Label>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
'@
$Reader = (New-Object System.XML.XMLNodeReader $XAML)
$FORM = [Windows.Markup.XAMLReader]::Load($Reader)
################################################
# Events
################################################
$FORM.FindName('AddRowGrid').Add_Click({
#write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Adding row..."
AddArray -Name "" -Parameter ""
UpdateScriptOutput
})
$FORM.FindName('DeleteRowGrid').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Deleting items from grid..."
$Global:CommandArray = @()
$Global:CommandArray += $FORM.FindName('CSVGrid').Itemssource | ? IsChecked -eq $False
$FORM.FindName('CSVGrid').ItemsSource = $Global:CommandArray
UpdateScriptOutput
})
$FORM.FindName('DeleteAllRows').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Deleting all items from grid..."
$Global:CommandArray = @()
$FORM.FindName('CSVGrid').ItemsSource = $Global:CommandArray
UpdateScriptOutput
})
$FORM.FindName('AddModuleFullToGridButton').Add_Click({
If ($FORM.FindName('FilterModuleComboBox').Text -eq "")
{
popup -Message "Please select a module"
}
else
{
If (($FORM.FindName('CSVGrid').ItemsSource | ? Module -eq $FORM.FindName('FilterModuleComboBox').Text | ? Name -eq "").Parameter.Count -eq 0)
{
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Adding items to grid..."
AddArray -Module $FORM.FindName('FilterModuleComboBox').Text -Name "" -Parameter "" -ValidateSet ""
UpdateScriptOutput
}
else
{
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Module is already in the grid, we're not adding it again..."
}
}
})
$FORM.FindName('AddModuleGetToGridButton').Add_Click({
If ($FORM.FindName('FilterModuleComboBox').Text -eq "")
{
popup -Message "Please select a module"
}
else
{
If (($FORM.FindName('CSVGrid').ItemsSource | ? Module -eq $FORM.FindName('FilterModuleComboBox').Text | ? Name -eq "Get-*").Parameter.Count -eq 0)
{
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Adding items to grid..."
AddArray -Module $FORM.FindName('FilterModuleComboBox').Text -Name "Get-*" -Parameter "" -ValidateSet ""
UpdateScriptOutput
}
else
{
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Module is already in the grid with Get-* cmdlets, we're not adding it again..."
}
}
})
$FORM.FindName('AddToGrid').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Adding items to grid..."
$PropertiesDiscarded = 0
$PropertiesAdded = 0
$PropertiesChecked = $false
Foreach ($Property in $FORM.FindName('PickPropertiesComboBox').Items)
{
If ($Property.PropertyChecked -eq $true)
{
$PropertiesChecked = $true
If (($FORM.FindName('CSVGrid').ItemsSource | ? Name -eq $FORM.FindName('PickCmdletComboBox').Text | ? Parameter -eq $Property.PropertyName).Parameter.Count -eq 0)
{
$command = (get-command $FORM.FindName('PickCmdletComboBox').Text)
$potentialvalues=@()
try {
If (($command.ResolveParameter($Property.PropertyName).ParameterType.Name) -eq "String")
{
$p=$command.Parametersets[0].parameters |?{$_.name -eq $Property.PropertyName}
$potentialvalues = ($p.Attributes).ValidValues
# Thanks http://blogs.msdn.com/b/powershell/archive/2006/05/10/594175.aspx?Redirected=true
}
else
{
$potentialvalues = [Enum]::GetNames($command.ResolveParameter($Property.PropertyName).ParameterType.FullName)
}
}
catch {}
AddArray -Module "" -Name $FORM.FindName('PickCmdletComboBox').Text -Parameter $Property.PropertyName -ValidateSet ($potentialvalues -join ";")
$PropertiesAdded = $PropertiesAdded +1
}
else
{$PropertiesDiscarded = $PropertiesDiscarded +1}
}
}
If ($PropertiesAdded -eq 0)
{
If (($FORM.FindName('CSVGrid').ItemsSource | ? Name -eq $FORM.FindName('PickCmdletComboBox').Text).Parameter.Count -eq 0)
{
AddArray -Name $FORM.FindName('PickCmdletComboBox').Text -Parameter ""
}
else
{write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] NOTE : The cmdlet was not added, because it was already found in the grid."}
}
If ($PropertiesDiscarded -gt 0) {write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] NOTE : $PropertiesDiscarded propertie(s) were not added, because they were already found in the grid."}
#write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Adding items to grid...done!"
UpdateScriptOutput
})
$FORM.FindName('AddRunbookToGrid').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Adding Runbooks to grid..."
$RunbooksToAdd = ""
Foreach ($Property in $FORM.FindName('PickRunbooksComboBox').Items)
{
If ($Property.RunbookChecked -eq $true)
{
If (($FORM.FindName('CSVGrid').ItemsSource | ? Name -eq $FORM.FindName('PickRunbooksComboBox').Text | ? Parameter -eq $Property.PropertyName).Parameter.Count -eq 0)
{
$RunbooksToAdd += ";" + $Property.RunbookName
}
}
}
$RunbooksToAdd = $RunbooksToAdd.Substring(1,$RunbooksToAdd.Length-1)
#We check if there are already some entries for the SMA cmdlets and, if yes, we add to them
If (($FORM.FindName('CSVGrid').ItemsSource | ? Name -eq "Start-SmaRunbook" | ? Parameter -eq "Name").Parameter.Count -ne 0)
{
$RunbookToAdd = (($RunbooksToAdd + ";" + ($FORM.FindName('CSVGrid').ItemsSource | ? Name -eq "Start-SmaRunbook" | ? Parameter -eq "Name").ValidateSet).Split(";") | select-object -unique) -join ";"
$Global:CommandArray = @()
$Global:CommandArray += $FORM.FindName('CSVGrid').Itemssource | ? Name -ne "Start-SmaRunbook"
$FORM.FindName('CSVGrid').ItemsSource = $Global:CommandArray
}
AddArray -Module "" -Name "Start-SmaRunbook" -Parameter "Name" -ValidateSet $RunbooksToAdd
AddArray -Module "" -Name "Start-SmaRunbook" -Parameter "Parameters" -ValidateSet ""
AddArray -Module "" -Name "Start-SmaRunbook" -Parameter "WebServiceEndpoint" -ValidateSet $Global:SMAWS
AddArray -Module "" -Name "Start-SmaRunbook" -Parameter "Port" -ValidateSet $Global:SMAPort
#AddArray -Module "" -Name "Stop-SmaRunbook" -Parameter "Name" -ValidateSet $RunbooksToAdd
UpdateScriptOutput
})
$FORM.FindName('PickCmdletComboBox').Add_DropDownClosed({
If ($FORM.FindName('PickCmdletComboBox').Text)
{
$SelectedCmdletParameters = (Get-Command $FORM.FindName('PickCmdletComboBox').Text | % parameters).keys
$Global:PropertyArray = @()
Foreach ($SelectedCmdletParameter in $SelectedCmdletParameters)
{
$tmpObject2 = select-object -inputobject "" PropertyChecked, PropertyName
$tmpObject2.PropertyChecked = $false
$tmpObject2.PropertyName = $SelectedCmdletParameter
$Global:PropertyArray += $tmpObject2
$FORM.FindName('PickPropertiesComboBox').ItemsSource = $Global:PropertyArray
$FORM.FindName('PickPropertiesComboBox').IsDropDownOpen = $true
}
}
})
$FORM.FindName('ImportCSVFile').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Importing..."
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Importing CSV File..."
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$OpenFileWindow = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileWindow.InitialDirectory = (Get-Location -PSProvider FileSystem).Path
$OpenFileWindow.ShowHelp=$false
$OpenFileWindow.Filter = "csv files (*.csv)|*.csv";
if($OpenFileWindow.ShowDialog() -eq "OK")
{
$CSVFileLocation = $OpenFileWindow.FileName.substring(0, $OpenFileWindow.FileName.LastIndexOf("\"))
$CSVFilName = ($OpenFileWindow.FileName.split("\")[$OpenFileWindow.FileName.split("\").Count-1]).split(".")[0]
$NewCSVData = Import-CSV $OpenFileWindow.FileName | Select-Object IsChecked, Module, Name, Parameter, ValidateSet, ValidatePattern
If ($FORM.FindName('ImportCSVFileAction').Text -eq "Replace grid content")
{$FORM.FindName('CSVGrid').ItemsSource = $NewCSVData}
else {$FORM.FindName('CSVGrid').ItemsSource += $NewCSVData}
Foreach ($Property in $FORM.FindName('CSVGrid').Itemssource) {$Property.IsChecked="False"}
$FORM.FindName('ToolkitNameTextBox').Text = $CSVFilName.split(".")[0]
UpdateScriptOutput
If (($FORM.FindName('ImportXMLCB').IsChecked) -and (Test-Path -Path ($CSVFileLocation + "\" + $FORM.FindName('ToolkitNameTextBox').Text + ".xml")))
{
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Importing delegation data file $XMLFile..."
$XmlReader= New-Object System.Xml.XmlTextReader(($CSVFileLocation + "\" + $FORM.FindName('ToolkitNameTextBox').Text + ".xml"))
While ($XmlReader.Read()){
If ($XmlReader.NodeType -eq [System.Xml.XmlNodeType]::Element){
switch ($XmlReader.Name){
"DelegationData" {$FORM.FindName('ConfigureAllowedUsersTextBox').Text=$XmlReader.ReadString()}
}
}
}
$XmlReader.Close()
If ($FORM.FindName('ConfigureAllowedUsersTextBox').Text)
{
$FORM.FindName('ConfigureAllowedUsersCB').IsChecked=$True
$FORM.FindName('ConfigureAllowedUsersTextBox').IsEnabled=$True
$FORM.FindName('UpdateDelegation').IsEnabled=$True
UpdateDelegation
}
}
else
{
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] No delegation data found or the option was not checked. The tool will not update the delegation text box."
}
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Importing...Done!"
}
else
{
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] No CSV file was specified by user, returning to main window..."
}
})
$FORM.FindName('ExportCSVFile').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Exporting..."
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Exporting to CSV File" $FORM.FindName('CSVFileNameTextBox').Text "..."
If ($FORM.FindName('CSVGrid').Itemssource)
{
$FORM.FindName('CSVGrid').Itemssource | export-csv ((Get-Location -PSProvider FileSystem).Path + "\" + $FORM.FindName('CSVFileNameTextBox').Text) -notypeinformation
popup -Message ("CSV file " + $FORM.FindName('CSVFileNameTextBox').Text + " was created in folder " + (Get-Location -PSProvider FileSystem).Path)
If ($FORM.FindName('ExportXMLCB').IsChecked)
{
$XMLFile = $FORM.FindName('CSVFileNameTextBox').Text.Substring(0, $FORM.FindName('CSVFileNameTextBox').Text.Length -4) + ".xml"
$AllowedUsers = $FORM.FindName('ConfigureAllowedUsersTextBox').Text
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Exporting delegation data to file $XMLFile..."
$XMLData =
@”
<DefaultConfiguration>
<DelegationData>$AllowedUsers</DelegationData>
</DefaultConfiguration>
“@
$XMLData | Out-File $XMLFile -Force
}
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Exporting...Done!"
}
else
{
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Grid is empty, file was not created."
popup -Message ("Grid is empty, file was not created.")
}
})
$FORM.FindName('FilterModuleButton').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Filtering cmdlet list for module" $FORM.FindName('FilterModuleComboBox').Text "..."
$Global:CmdletList = Get-Command -Module $FORM.FindName('FilterModuleComboBox').Text | Sort-Object Name | Select Name
UpdateCmdletList
})
$FORM.FindName('RemoveFilterModuleButton').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Removing filter on cmdlet list..."
$Global:CmdletList = Get-Command | Sort-Object Name | Select Name
UpdateCmdletList
})
$FORM.FindName('ImportModuleButton').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Importing module" $FORM.FindName('ImportModuleTextBox').Text "..."
$eap = $ErrorActionPreference = "SilentlyContinue"
Import-Module $FORM.FindName('ImportModuleTextBox').Text
if (!$?) {
$ErrorActionPreference =$eap
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : Module" $FORM.FindName('ImportModuleTextBox').Text "could not be imported. Please check module name and existence." }
popup -Message ("WARNING : Module " + $FORM.FindName('ImportModuleTextBox').Text + " could not be imported. Please check module name and existence.")
else{
$ErrorActionPreference =$eap
$Global:CmdletList = Get-Command | Sort-Object Name | Select Name
UpdateCmdletList
UpdateModuleList
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Importing module" $FORM.FindName('ImportModuleTextBox').Text "...Done!"
popup -Message ("Module " + $FORM.FindName('ImportModuleTextBox').Text + " was imported.")
}
})
$FORM.FindName('CopyToClipboard').Add_Click({
$null = [Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
$dataObject = New-Object windows.forms.dataobject
$dataObject.SetData([Windows.Forms.DataFormats]::UnicodeText, $true, $FORM.FindName('ScriptOutputTextBlock').Text)
[Windows.Forms.Clipboard]::SetDataObject($dataObject, $true)
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Copied script content to clipboard."
popup -Message "Script content was copied to clipboard."
})
$FORM.FindName('ConfigureAllowedUsersCB').Add_Checked({
$FORM.FindName('ConfigureAllowedUsersTextBox').IsEnabled=$True
$FORM.FindName('UpdateDelegation').IsEnabled=$True
})
$FORM.FindName('ConfigureAllowedUsersCB').Add_UnChecked({
$FORM.FindName('ConfigureAllowedUsersTextBox').IsEnabled=$False
$FORM.FindName('UpdateDelegation').IsEnabled=$false
})
$FORM.FindName('ToolkitNameTextBox').Add_TextChanged({
$FORM.FindName('CSVFileNameTextBox').Text = $FORM.FindName('ToolkitNameTextBox').Text + ".csv"
UpdateScriptOutput
})
$FORM.FindName('UpdateDelegation').Add_Click({
UpdateDelegation
})
$FORM.FindName('RemoveToolkit').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Running toolkit removal script..."
If (Get-DscConfiguration | ? Name -eq ($FORM.FindName('ToolkitNameTextBox').Text + "EP") | ? Ensure -eq "Present")
{
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Toolkit endpoint was found as both present and active on this local machine, we can proceed with the removal."
$CommandToExecute = $FORM.FindName('ScriptOutputTextBlock').Text
$CommandToExecute = $CommandToExecute.Replace("#CleanAll", "CleanAll")
$CommandToExecute = $CommandToExecute.Replace("#" + $FORM.FindName('ToolkitNameTextBox').Text, $FORM.FindName('ToolkitNameTextBox').Text)
$CommandToExecute = $CommandToExecute.Replace("#Start-DscConfiguration", "Start-DscConfiguration")
If ((-not ($FORM.FindName('FilterModuleComboBox').Items -contains "Microsoft.SystemCenter.ServiceManagementAutomation")) -and ($FORM.FindName('BypassSMACmdletsNotPresentCB').IsChecked -eq $true))
{
#write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : SMA module was not found on the local machine. Removal script will be updated before actual removal."
$CommandToExecute = $CommandToExecute -replace ",Start-SmaRunbook,(.+?),(.+?),(.+?)", ""
$CommandToExecute = $CommandToExecute -replace ",Start-SmaRunbook,(.+?),,", ""
#$CommandToExecute = $CommandToExecute -replace ",Stop-SmaRunbook,(.+?),(.+?),(.+?)", ""
}
#write-host ($CommandToExecute)
write-host (Invoke-Expression ($CommandToExecute) -Verbose)
}
else
{
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : This toolkit was not found as both present and active on this local machine. We'll not be trying to remove it."
}
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Running toolkit removal script...done!"
})
$FORM.FindName('ImportToolkit').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Running toolkit import script..."
$CommandToExecute = $FORM.FindName('ScriptOutputTextBlock').Text
$CommandToExecute = $CommandToExecute.Replace("#" + $FORM.FindName('ToolkitNameTextBox').Text, $FORM.FindName('ToolkitNameTextBox').Text)
$CommandToExecute = $CommandToExecute.Replace("#Start-DscConfiguration", "Start-DscConfiguration")
If (-not ($FORM.FindName('FilterModuleComboBox').Items -contains "Microsoft.SystemCenter.ServiceManagementAutomation"))
{
If ($FORM.FindName('BypassSMACmdletsNotPresentCB').IsChecked -eq $true)
{
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : SMA module was not found on the local machine. Test toolkit will still be imported, but SMA cmdlets will be disabled."
$CommandToExecute = $CommandToExecute -replace ",Start-SmaRunbook,(.+?),(.+?),(.+?)", ""
$CommandToExecute = $CommandToExecute -replace ",Start-SmaRunbook,(.+?),,", ""
#$CommandToExecute = $CommandToExecute -replace ",Stop-SmaRunbook,(.+?),(.+?),(.+?)", ""
write-host (Invoke-Expression ($CommandToExecute) -Verbose)
popup -Message ("Toolkit was imported, please wait about 30 seconds before trying a test, while WinRM restarts")
}
else
{
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : SMA module was not found on the local machine. Test toolkit will not be imported, as the bypass checkbox is not enabled in the tool."
}
}
else
{
write-host (Invoke-Expression ($CommandToExecute) -Verbose)
popup -Message ("Toolkit was imported, please wait about 30 seconds before trying a test, while WinRM restarts")
}
#write-host $CommandToExecute
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Running toolkit import script...done!"
})
$FORM.FindName('TestToolkit').Add_Click({
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Testing toolkit..."
If (Get-DscConfiguration | ? Name -eq ($FORM.FindName('ToolkitNameTextBox').Text + "EP") | ? Ensure -eq "Present")
{
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Toolkit endpoint was found as both present and active on this local machine, we can proceed with the test."
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Connecting to JEA session (with current logged on user)...This may take a few seconds..."
write-host (Invoke-Expression ("`$s = New-PSSession -cn . -ConfigurationName " + $FORM.FindName('ToolkitNameTextBox').Text +"EP"))
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Displaying available cmdlets..."
write-host (Invoke-Expression ("Invoke-command `$s {get-command} | out-string"))
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] Exiting JEA session..."
write-host (Invoke-Expression ("Remove-PSSession `$s"))
}
else
{
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : This toolkit was not found as both present and active on this local machine. Please make sure it is already imported, either manually or via the tool."
}
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Testing toolkit...done!"
})
########################################################################################
#Make sure we run elevated, or relaunch as admin
########################################################################################
$CurrentScriptDirectory = $PSCommandPath.Substring(0,$PSCommandPath.LastIndexOf("\"))
Set-Location $CurrentScriptDirectory
#Thanks to http://gallery.technet.microsoft.com/scriptcenter/63fd1c0d-da57-4fb4-9645-ea52fc4f1dfb
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if (-not $IsAdmin)
{
try
{
$ScriptToLaunch = (Get-Location -PSProvider FileSystem).Path + "\JEAToolkitHelper.ps1"
$arg = "-file `"$($ScriptToLaunch)`""
write-host -ForegroundColor yellow "["(date -format "HH:mm:ss")"] WARNING : This script should run with administrative rights - Relaunching the script in elevated mode in 3 seconds..."
start-sleep 3
Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'
}
catch
{
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] Error : Failed to restart script with administrative rights - please make sure this script is launched elevated."
break
}
exit
}
else
{
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"] We are running in elevated mode, we can proceed with launching the tool."
}
################################################
# Main
################################################
write-host -ForegroundColor green "["(date -format "HH:mm:ss")"] JEA Toolkit Helper v$ToolVersion"
$Global:SDDLAccountList = @()
$Global:strSDDL = $Global:DefaultSDDL
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] Checking parameters and prerequisites..."
$Global:SMAIntegration = $false
$Global:SMAWS = $SMAEndPointWS
$Global:SMAPort = $SMAEndPointPort
If ($SMAWS)
{
$Global:FullRunbookList = @()
$Global:FullRunbookList = Invoke-Command -ScriptBlock {
param ($WS,$Port)
get-smarunbook -WebServiceEndpoint $WS -Port $Port | select RunbookName, RunbookID, Tags, Description
$runbooks
} -ArgumentList $Global:SMAWS, $Global:SMAPort -ComputerName ($Global:SMAWS).split("//")[2]
#write-host $Global:FullRunbookList
If (($Global:FullRunbookList.Count -eq 0) -or ($Global:FullRunbookList -eq $mull) -or ($Global:FullRunbookList -eq ""))
{
write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : Could not connect to SMA server or no runbooks were found. SMA integration will be disabled this time"
$Global:SMAIntegration = $false
}
else
{
$PropertyArray = @()
Foreach ($Runbook in $Global:FullRunbookList)
{
$tmpObject2 = select-object -inputobject "" RunbookChecked, RunbookName
$tmpObject2.RunbookChecked = $false
$tmpObject2.RunbookName = $Runbook.RunbookName
$PropertyArray += $tmpObject2
$FORM.FindName('PickRunbooksComboBox').ItemsSource = $PropertyArray
}
write-host -ForegroundColor gray "["(date -format "HH:mm:ss")"]" $Global:FullRunbookList.Count "Runbooks were retrieved for SMA integration."
$Global:SMAIntegration = $true
}
}
else
{
write-host -ForegroundColor white "["(date -format "HH:mm:ss")"] No SMA server specified. SMA integration will not be available this time."
$Global:SMAIntegration = $false
}
If ($Global:SMAIntegration -eq $false)
{
$FORM.FindName('PickRunbookLabelContainer').IsEnabled = $false
$FORM.FindName('PickRunbooksComboBox').IsEnabled = $false
$FORM.FindName('AddRunbookToGrid').IsEnabled = $false
}
$Global:PropertyArray = New-Object System.Collections.ArrayList
$Global:CmdletList = Get-Command | Sort-Object Name | Select Name
UpdateCmdletList
UpdateModuleList
If (-not ($FORM.FindName('FilterModuleComboBox').Items -contains "Microsoft.SystemCenter.ServiceManagementAutomation"))
{write-host -ForegroundColor red "["(date -format "HH:mm:ss")"] WARNING : SMA module was not found on the local machine. Test toolkit import may only be limited to non-SMA cmdlets."}
$FORM.FindName('ImportCSVFileAction').Items.Add("Replace grid content") | out-null
$FORM.FindName('ImportCSVFileAction').Items.Add("Add to grid content") | out-null
$FORM.FindName('ImportCSVFileAction').Text = "Replace grid content"
$Global:CommandArray = New-Object System.Collections.ArrayList
$Global:CommandArray = @()
write-host -ForegroundColor green "["(date -format "HH:mm:ss")"] Displaying GUI..."
$FORM.ShowDialog() | Out-Null
write-host -ForegroundColor green "["(date -format "HH:mm:ss")"] Exiting GUI..."