forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPester.Command.Cmdlets.LocalAccounts.LocalGroup.Tests.ps1
More file actions
921 lines (748 loc) · 31.8 KB
/
Copy pathPester.Command.Cmdlets.LocalAccounts.LocalGroup.Tests.ps1
File metadata and controls
921 lines (748 loc) · 31.8 KB
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
# This is a Pester test suite to validate the cmdlets in LocalAccounts module
#
# Copyright (c) Microsoft Corporation, 2015
function RemoveTestGroups
{
param([string] $basename)
$results = Get-LocalGroup $basename*
foreach ($element in $results) {
Remove-LocalGroup -SID $element.SID
}
}
function VerifyFailingTest
{
param(
[scriptblock] $sb,
[string] $expectedFqeid
)
$backupEAP = $script:ErrorActionPreference
$script:ErrorActionPreference = "Stop"
try {
& $sb
throw "Expected FullyQualifiedErrorId: $expectedFqeid"
}
catch {
$_.FullyQualifiedErrorId | Should Be $expectedFqeid
}
finally {
$script:ErrorActionPreference = $backupEAP
}
}
try {
#skip all tests on non-windows platform
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$IsNotSkipped = ($IsWindows -eq $true);
$PSDefaultParameterValues["it:skip"] = !$IsNotSkipped
Describe "Verify Expected LocalGroup Cmdlets are present" -Tags "CI" {
It "Test command presence" {
$result = Get-Command -Module Microsoft.PowerShell.LocalAccounts | % Name
$result -contains "New-LocalGroup" | Should Be $true
$result -contains "Set-LocalGroup" | Should Be $true
$result -contains "Get-LocalGroup" | Should Be $true
$result -contains "Rename-LocalGroup" | Should Be $true
$result -contains "Remove-LocalGroup" | Should Be $true
}
}
Describe "Validate simple New-LocalGroup" -Tags @('CI', 'RequireAdminOnWindows') {
AfterEach {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupAddRemove
}
}
It "Creates New-LocalGroup using only name" {
$result = New-LocalGroup -Name TestGroupAddRemove
$result.Name | Should BeExactly TestGroupAddRemove
$result.ObjectClass | Should Be Group
}
}
Describe "Validate New-LocalGroup cmdlet" -Tags @('Feature', 'RequireAdminOnWindows') {
AfterEach {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupAddRemove
}
}
It "Creates New-LocalGroup with name and description" {
$result = New-LocalGroup -Name TestGroupAddRemove -Description "Test Group New 1 Description"
$result.Name | Should BeExactly TestGroupAddRemove
$result.Description | Should BeExactly "Test Group New 1 Description"
$result.SID | Should Not BeNullOrEmpty
$result.ObjectClass | Should Be Group
}
It "Errors on New-LocalGroup with name collision" {
$sb = {
New-LocalGroup TestGroupAddRemove
return New-LocalGroup TestGroupAddRemove
}
VerifyFailingTest $sb "GroupExists,Microsoft.PowerShell.Commands.NewLocalGroupCommand"
}
It "Can use SID for group name" {
$sidName = "S-1-5-21-3949576937-491355012-4054854628-1053"
try {
$result = New-LocalGroup -Name $sidName
$result | Should Not BeNullOrEmpty
$result.Name | Should BeExactly $sidName
$result.SID | Should Not BeExactly $sidName
$result.ObjectClass | Should Be Group
}
finally {
RemoveTestGroups -basename $sidName
}
}
It "Errors on empty group name" {
$sb = { New-LocalGroup -Name "" }
VerifyFailingTest $sb "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.NewLocalGroupCommand"
}
It "Creates New-LocalGroup with name(256) at max" {
$nameMax = "A"*256
$desc = "D"*48
try {
$result = New-LocalGroup -Name $nameMax -Description $desc
$result.Name | Should BeExactly $nameMax
$result.Description | Should BeExactly $desc
$result.SID | Should Not BeNullOrEmpty
$result.ObjectClass | Should Be Group
}
finally {
RemoveTestGroups -basename $nameMax
}
}
It "Errors on New-LocalGroup with name(256) over max" {
$name = "A"*257
$desc = "D"*129
try {
$shouldBeNull = New-LocalGroup -Name $name -Description $desc
throw "An error was expected"
}
catch {
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.NewLocalGroupCommand"
}
finally {
#clean up erroneous creation
if ($shouldBeNull) { Remove-LocalGroup -Name $name }
}
}
It "Creates New-LocalGroup with Description > 48 characters" {
$descMax = "Test Group Add Description that is longer than 48 characters"
$result = New-LocalGroup -Name TestGroupAddRemove -Description $descMax
$result.Description | Should BeExactly $descMax
}
It "Errors on Invalid characters" {
#Arrange
#list of characters that should be invalid
$InvalidCharacters = @"
\/"[]:|<>+=;,?*
"@
$failedCharacters = @()
$InvalidCharacters = $InvalidCharacters[0..($InvalidCharacters.Length - 1)]
#Act
foreach ($character in $InvalidCharacters) {
try {
$invalidGroup = New-LocalGroup -Name ("InvalidBecauseOf" + $character) -ErrorAction Stop
}
catch {
}
finally {
if ($invalidGroup) {
Remove-LocalGroup -Name $invalidGroup
$failedCharacters += $character
}
}
}
if ($failedCharacters.Count -gt 0) { Write-Host "characters causing test fail: $failedCharacters" }
$failedCharacters.Count -eq 0 | Should Be true
}
It "Error on names containing only spaces" {
$sb = { New-LocalGroup -Name " " }
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.NewLocalGroupCommand"
}
It "Error on names containing only periods" {
$sb = {
New-LocalGroup -Name "..."
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.NewLocalGroupCommand"
}
It "Errors on names ending in a period" {
$sb = {
New-LocalGroup -Name "TestEndInPeriod."
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.NewLocalGroupCommand"
$sb = {
New-LocalGroup -Name ".TestEndIn.Period.."
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.NewLocalGroupCommand"
}
It "Errors on Name over 256 characters" {
$sb = { New-LocalGroup -Name ("A"*257) }
try {
VerifyFailingTest $sb "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.NewLocalGroupCommand"
}
finally {
RemoveTestGroups -basename ("A"*257)
}
}
}
Describe "Validate simple Get-LocalGroup" -Tags @('CI', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsNotSkipped) {
New-LocalGroup -Name TestGroupGet1 -Description "Test Group Get 1 Description" | Out-Null
}
}
AfterAll {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupGet
}
}
It "Can Get-LocalGroup by specific group name" {
$result = Get-LocalGroup TestGroupGet1
$result.Name | Should Be "TestGroupGet1"
$result.Description | Should Be "Test Group Get 1 Description"
$result.ObjectClass | Should Be "Group"
}
}
Describe "Validate Get-LocalGroup cmdlet" -Tags @('Feature', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsNotSkipped) {
New-LocalGroup -Name TestGroupGet1 -Description "Test Group Get 1 Description" | Out-Null
New-LocalGroup -Name TestGroupGet2 -Description "Test Group Get 2 Description" | Out-Null
}
}
AfterAll {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupGet
}
}
#Note: this test is no longer in the test plan
It "Can Get-LocalGroup of all groups" {
$result = Get-LocalGroup
$result.Count -gt 2 | Should Be true
}
It "Can Get-LocalGroup of a specific group by SID" {
$result = Get-LocalGroup TestGroupGet1
$resultBySID = Get-LocalGroup -SID $result.SID
$resultBySID.SID | Should Not BeNullOrEmpty
$resultBySID.Name | Should Be TestGroupGet1
}
It "Can Get-LocalGroup of a well-known group by SID string" {
$sid = New-Object System.Security.Principal.SecurityIdentifier -ArgumentList BG
$guestGroup = Get-LocalGroup -SID BG
$guestGroup.SID | Should Be $sid.Value
}
It "Can Get-LocalGroup by wildcard" {
$result = Get-LocalGroup TestGroupGet*
$result.Count -eq 2 | Should Be true
$result.Name -contains "TestGroupGet1" | Should Be true
$result.Name -contains "TestGroupGet2" | Should Be true
}
It "Can Get-LocalGroup gets by array of names" {
$result = Get-LocalGroup @("TestGroupGet1", "TestGroupGet2")
$result.Count -eq 2 | Should Be true
$result.Name -contains "TestGroupGet1" | Should Be true
$result.Name -contains "TestGroupGet2" | Should Be true
}
It "Can Get-LocalGroups by array of SIDs" {
$sid1 = (Get-LocalGroup TestGroupGet1).SID
$sid2 = (Get-LocalGroup TestGroupGet2).SID
$result = Get-LocalGroup -SID @($sid1, $sid2)
$result.Count -eq 2 | Should Be true
$result.Name -contains "TestGroupGet1" | Should Be true
$result.Name -contains "TestGroupGet2" | Should Be true
}
It "Can Get-LocalGroups by pipe of an array of Group objects" {
$testGroups = Get-LocalGroup TestGroupGet*
$result = @($testGroups, $testGroups) | Get-LocalGroup
$result.Count -eq 4 | Should Be true
$result.Name -contains "TestGroupGet1" | Should Be true
$result.Name -contains "TestGroupGet2" | Should Be true
}
It "Can respond to -ErrorAction Stop" {
$result = $null
try {
Get-LocalGroup @("TestGroupGet1", "TestGroupGetNameThatDoesntExist1", "TestGroupGetNameThatDoesntExist2") -ErrorAction Stop -ErrorVariable outErr -OutVariable outOut | Out-Null
}
catch {
$result = @($outErr.Count, $outErr[0].ErrorRecord.CategoryInfo.Reason, $outOut.Name)
}
if ($result -eq $null)
{
# Force failing the test because an unexpected outcome occurred
$false | Should Be $true
}
else
{
$result[0] -eq 1 | Should Be true
$result[1] -match "GroupNotFound" | Should Be true
$result[2] -match "TestGroupGet1" | Should Be true
}
}
It "Errors on Get-LocalGroup by an invalid group name" {
$sb = {
Get-LocalGroup 'TestGroupGetNameThatDoesntExist'
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.GetLocalGroupCommand"
}
It "Errors on Get-LocalGroup by an invalid group SID" {
$sb = {
$result = New-LocalGroup -Name TestGroupGet3 -Description "Test Group Get 3 Description"
Remove-LocalGroup TestGroupGet3
Get-LocalGroup -SID $result.SID
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.GetLocalGroupCommand"
}
It "Can get no local groups if none match wildcard" {
$localGroupName = 'TestGroupGetNameThatDoesntExist'
$result = (Get-LocalGroup $localGroupName*).Count
$result -eq 0 | Should Be true
}
}
Describe "Validate simple Set-LocalGroup" -Tags @('CI', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsNotSkipped) {
$group1SID = ""
}
}
BeforeEach {
if ($IsNotSkipped) {
$group1SID = (New-LocalGroup -Name TestGroupSet1).SID
}
}
AfterEach {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupSet
$group1SID = ""
}
}
It "Can Set-LocalGroup by name" {
Set-LocalGroup -Name TestGroupSet1 -Description "Test Group Set 1 new description"
$result = Get-LocalGroup -Name TestGroupSet1
$result.Description | Should BeExactly "Test Group Set 1 new description"
}
}
Describe "Validate Set-LocalGroup cmdlet" -Tags @('Feature', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsNotSkipped) {
$group1SID = ""
}
}
BeforeEach {
if ($IsNotSkipped) {
$group1SID = (New-LocalGroup -Name TestGroupSet1).SID
}
}
AfterEach {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupSet
$group1SID = ""
}
}
It "Can Set-LocalGroup by SID" {
Set-LocalGroup -SID $group1SID -Description "Test Group Set 1 newer description"
$result = Get-LocalGroup -Name TestGroupSet1
$result.Description | Should BeExactly "Test Group Set 1 newer description"
}
It "Can Set-LocalGroup using -InputObject" {
$group = Get-LocalGroup TestGroupSet1
Set-LocalGroup -InputObject $group -Description "Test Group Set 1 newer still description"
$result = Get-LocalGroup TestGroupSet1
$result.Description | Should BeExactly "Test Group Set 1 newer still description"
}
It "Can Set-LocalGroup using pipeline" {
Get-LocalGroup TestGroupSet1 | Set-LocalGroup -Description "Test Group Set 1 newer still description"
$result = Get-LocalGroup TestGroupSet1
$result.Description | Should BeExactly "Test Group Set 1 newer still description"
}
It "Errors on Set-LocalGroup without specifying a Group" {
$sb = {
Set-LocalGroup -Description "Test Group Set 1 newer still description"
}
VerifyFailingTest $sb "AmbiguousParameterSet,Microsoft.PowerShell.Commands.SetLocalGroupCommand"
}
It "Errors on Set-LocalGroup with an invalid Group name" {
$sb = {
Set-LocalGroup -Name "NonexistantGroupName" -Description "Test Group Set 1 newer still description"
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.SetLocalGroupCommand"
}
It "Errors on Set-LocalGroup with an invalid Group SID" {
$sb = {
Set-LocalGroup -SID "S-1-5-21-1220945662-555555555-555555555-5555" -Description "Test Group Set 1 newer still description"
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.SetLocalGroupCommand"
}
It "Can Set-LocalGroup with description over 48 characters" {
$desc = "A"*129
Set-LocalGroup -Name TestGroupSet1 -Description $desc
$result = Get-LocalGroup -Name TestGroupSet1
$result.Description | Should BeExactly $desc
}
}
Describe "Validate simple Rename-LocalGroup" -Tags @('CI', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsNotSkipped) {
$group1SID = ""
$group2SID = (New-LocalGroup -Name TestGroupRename2 -Description "Test Group Rename 2 Description" ).SID
}
}
BeforeEach {
if ($IsNotSkipped) {
$group1SID = ( New-LocalGroup -Name TestGroupRename1 -Description "Test Group Rename 1 Description" ).SID
}
}
AfterEach {
if ($IsNotSkipped) {
Remove-LocalGroup -SID $group1SID
$group1SID = ""
}
}
AfterAll {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupRename
}
}
It "Can Rename-LocalGroup using a valid group name" {
$group1SID | Should Not BeNullOrEmpty
Rename-LocalGroup TestGroupRename1 TestGroupRename1x
$result = Get-LocalGroup -SID $group1SID
$result.Name | Should BeExactly TestGroupRename1x
}
}
Describe "Validate Rename-LocalGroup cmdlet" -Tags @('Feature', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsNotSkipped) {
$group1SID = ""
$group2SID = (New-LocalGroup -Name TestGroupRename2 -Description "Test Group Rename 2 Description" ).SID
}
}
BeforeEach {
if ($IsNotSkipped) {
$group1SID = (New-LocalGroup -Name TestGroupRename1 -Description "Test Group Rename 1 Description" ).SID
}
}
AfterEach {
if ($IsNotSkipped) {
Remove-LocalGroup -SID $group1SID
$group1SID = ""
}
}
AfterAll {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupRename
}
}
It "Can Rename-LocalGroup using a valid group SID" {
$group1SID | Should Not BeNullOrEmpty
Rename-LocalGroup -SID $group1SID TestGroupRename1x
$result = Get-LocalGroup -SID $group1SID
$result.Name | Should BeExactly TestGroupRename1x
}
It "Can Rename-LocalGroup using a valid group -InputObject" {
$group1SID | Should Not BeNullOrEmpty
$group = Get-LocalGroup TestGroupRename1
Rename-LocalGroup -InputObject $group -NewName TestGroupRename1x
$result = Get-LocalGroup -SID $group1SID
$result.Name | Should BeExactly TestGroupRename1x
}
It "Can Rename-LocalGroup using a valid group sent using pipeline" {
$group1SID | Should Not BeNullOrEmpty
Get-LocalGroup TestGroupRename1 | Rename-LocalGroup -NewName TestGroupRename1x
$result = Get-LocalGroup -SID $group1SID
$result.Name | Should BeExactly TestGroupRename1x
}
It "Errors on Rename-LocalGroup without specifying a Group" {
$sb = {
Rename-LocalGroup
}
VerifyFailingTest $sb "AmbiguousParameterSet,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
}
It "Errors onRename-LocalGroup nonexistant group name" {
$sb = {
Rename-LocalGroup nonexistantGroupName -NewName DummyNewName
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
}
It "Errors onRename-LocalGroup nonexistant group SID" {
$nonexistantSid =
$sb = {
Rename-LocalGroup -SID "S-1-5-21-1220945662-555555555-555555555-5555" -NewName DummyNewName
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
}
It "Errors on Rename-LocalGroup Renames a valid group to already existing name" {
$newName = "TestGroupRename2"
$sb = {
Rename-LocalGroup TestGroupRename1 $newName
}
VerifyFailingTest $sb "NameInUse,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
$group1Name = (Get-LocalGroup -SID $group1SID).Name
$group2Name = (Get-LocalGroup -SID $group2SID).Name
$group1Name | Should BeExactly TestGroupRename1
$group2Name | Should BeExactly $newName
}
It "Errors on Invalid characters" {
#Arrange
#list of characters that should be invalid
$InvalidCharacters = @"
\/"[]:|<>+=;,?*
"@
$InvalidCharacters = $InvalidCharacters[0..($InvalidCharacters.Length - 1)]
$failedCharacters = @()
#Act
foreach ($character in $InvalidCharacters) {
try {
Rename-LocalGroup -Name TestGroupRename1 -NewName ("InvalidBecauseOf" + $character) -ErrorAction Stop
$invalidGroup = (Get-LocalGroup ("InvalidBecauseOf" + $character))
}
catch {
}
finally {
#handle groups being erroneously renamed
if ($invalidGroup) {
Rename-LocalGroup -Name ("InvalidBecauseOf" + $character) -NewName TestGroupRename1
$failedCharacters += $character
}
}
}
#Assert
if ($failedCharacters.Count -gt 0) { Write-Host "characters causing test fail: $failedCharacters" }
$failedCharacters.Count -eq 0 | Should Be true
}
It "Error on names containing only spaces" {
$sb = {
Rename-LocalGroup -Name TestGroupRename1 -NewName " "
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
}
It "Error on names containing only periods" {
$sb = {
Rename-LocalGroup -Name TestGroupRename1 -NewName "..."
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
}
It "Errors on names ending in a period" {
$sb = {
Rename-LocalGroup -Name TestGroupRename1 -NewName "TestEndInPeriod."
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
$sb = {
Rename-LocalGroup -Name TestGroupRename1 -NewName ".TestEndIn.Period.."
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
}
It "Errors on Rename-LocalGroup using a valid group but invalid -NewName" {
$sb = {
Rename-LocalGroup -Name TestGroupRename1 -NewName "TestGroupRename<>1x"
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
}
It "Can Rename-LocalGroup using a valid group name at max length 256" {
$newName = "A"*256
Rename-LocalGroup TestGroupRename1 $newName
$result = Get-LocalGroup -SID $group1SID
$result.Name | Should BeExactly $newName
}
It "Errors on Rename-LocalGroup using a valid group name over max length 256" {
$newName = "A"*257
$sb = {
Rename-LocalGroup TestGroupRename1 $newName
}
VerifyFailingTest $sb "InvalidName,Microsoft.PowerShell.Commands.RenameLocalGroupCommand"
(Get-LocalGroup -SID $group1SID).Name | Should BeExactly TestGroupRename1
}
}
Describe "Validate simple Remove-LocalGroup" -Tags @('CI', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsNotSkipped) {
$group1SID = ""
}
}
BeforeEach {
if ($IsNotSkipped) {
$group1SID = (New-LocalGroup -Name TestGroupRemove1 -Description "Test Group Remove 1 Description" ).SID
}
}
AfterEach {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupRemove
$group1SID = ""
}
}
It "Can Remove-LocalGroup by name" {
$initialCount = (Get-LocalGroup).Count
$initialCount -gt 1 | Should Be true
$removeResult = Remove-LocalGroup TestGroupRemove1 2>&1
$removeResult | Should BeNullOrEmpty
$sb = {
Get-LocalGroup -SID $group1SID
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.GetLocalGroupCommand"
$finalCount = (Get-LocalGroup).Count
$initialCount -eq $finalCount + 1 | Should Be true
}
}
Describe "Validate Remove-LocalGroup cmdlet" -Tags @('Feature', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsNotSkipped) {
$group1SID = ""
$group2SID = ""
function VerifyBasicRemoval {
param (
[scriptblock]$removalAction
)
$initialCount = (Get-LocalGroup).Count
$initialCount -gt 1 | Should Be true
& $removalAction
$sb = {
Get-LocalGroup -SID $group1SID
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.GetLocalGroupCommand"
$finalCount = (Get-LocalGroup).Count
$initialCount -eq $finalCount + 1 | Should Be true
}
function VerifyArrayRemoval {
param (
[scriptblock]$removalAction
)
$initialCount = (Get-LocalGroup).Count
$initialCount -gt 1 | Should Be $true
& $removalAction
$sb = {
Get-LocalGroup -SID $group1SID
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.GetLocalGroupCommand"
$sb = {
Get-LocalGroup -SID $group2SID
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.GetLocalGroupCommand"
$finalCount = (Get-LocalGroup).Count
$initialCount -eq $finalCount + 2 | Should Be $true
}
}
}
BeforeEach {
if ($IsNotSkipped) {
$group1SID = [String](New-LocalGroup -Name TestGroupRemove1 -Description "Test Group Remove 1 Description" 2>&1).SID
$group2SID = [String](New-LocalGroup -Name TestGroupRemove2 -Description "Test Group Remove 2 Description" 2>&1).SID
}
}
AfterEach {
if ($IsNotSkipped) {
RemoveTestGroups -basename TestGroupRemove
$group1SID = ""
$group2SID = ""
}
}
It "Can Remove-LocalGroup by SID" {
$sb = {
$removeResult = Remove-LocalGroup -SID $group1SID 2>&1
$removeResult | Should BeNullOrEmpty
}
VerifyBasicRemoval $sb
}
It "Can Remove-LocalGroup using -InputObject" {
$sb = {
$group = Get-LocalGroup TestGroupRemove1
$removeResult = Remove-LocalGroup -InputObject $group 2>&1
$removeResult | Should BeNullOrEmpty
}
VerifyBasicRemoval $sb
}
It "Can Remove-LocalGroup using pipeline" {
$sb = {
$removeResult = Get-LocalGroup TestGroupRemove1 | Remove-LocalGroup 2>&1
$removeResult | Should BeNullOrEmpty
}
VerifyBasicRemoval $sb
}
It "Can Remove-LocalGroup by array of names" {
$sb = {
$removeResult = Remove-LocalGroup @("TestGroupRemove1","TestGroupRemove2") 2>&1
$removeResult | Should BeNullOrEmpty
}
VerifyArrayRemoval $sb
}
It "Can Remove-LocalGroup by array of SIDs" {
$sb = {
$removeResult = Remove-LocalGroup -SID @($group1SID, $group2SID) 2>&1
$removeResult | Should BeNullOrEmpty
}
VerifyArrayRemoval $sb
}
It "Can Remove-LocalGroup by array using -InputObject" {
$sb = {
$groups = Get-LocalGroup -Name @("TestGroupRemove1","TestGroupRemove2")
$removeResult = Remove-LocalGroup -InputObject $groups 2>&1
$removeResult | Should BeNullOrEmpty
}
VerifyArrayRemoval $sb
}
It "Can Remove-LocalGroup by array using pipeline" {
$sb = {
$removeResult = Get-LocalGroup -Name @("TestGroupRemove1","TestGroupRemove2") | Remove-LocalGroup 2>&1
$removeResult | Should BeNullOrEmpty
}
VerifyArrayRemoval $sb
}
It "Errors on Remove-LocalGroup without specifying a Name or SID" {
$sb = { Remove-LocalGroup }
VerifyFailingTest $sb "AmbiguousParameterSet,Microsoft.PowerShell.Commands.RemoveLocalGroupCommand"
}
It "Can Remove-LocalGroup with members" {
New-LocalUser TestUserRemove1 -NoPassword | Out-Null
Add-LocalGroupMember TestGroupRemove1 -Member TestUserRemove1 | Out-Null
$initialCount = (Get-LocalGroup).Count
$initialCount -gt 1 | Should Be true
$removeResult = Remove-LocalGroup TestGroupRemove1 2>&1
$removeResult | Should BeNullOrEmpty
#clean-up
Remove-LocalUser TestUserRemove1 | Out-Null
$sb = {
Get-LocalGroup TestGroupRemove1
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.GetLocalGroupCommand"
$finalCount = (Get-LocalGroup).Count
$initialCount -eq $finalCount + 1 | Should Be true
}
It "Errors on Remove-LocalGroup by invalid name" {
$initialCount = (Get-LocalGroup).Count
$initialCount -gt 1 | Should Be true
$sb = {
Remove-LocalGroup TestGroupRemove1NameThatDoesntExist
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.RemoveLocalGroupCommand"
$finalCount = (Get-LocalGroup).Count
$initialCount -eq $finalCount | Should Be true
}
It "Errors on Remove-LocalGroup by invalid SID" {
$initialCount = (Get-LocalGroup).Count
$initialCount -gt 1 | Should Be true
$sb = {
Remove-LocalGroup -SID $group1SID
Remove-LocalGroup -SID $group1SID
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.RemoveLocalGroupCommand"
$finalCount = (Get-LocalGroup).Count
$initialCount -eq $finalCount + 1 | Should Be $true
}
It "Can respond to -ErrorAction Stop" {
$errCount = 0
$fqeid = ""
try {
Remove-LocalGroup @("TestGroupRemove1", "TestGroupRemoveNameThatDoesntExist1", "TestGroupRemoveNameThatDoesntExist2") -ErrorAction Stop -ErrorVariable outError
}
catch {
$errCount = $outError.Count
$fqeid = $_.FullyQualifiedErrorId
}
# Confirm that the expected errors were caught
$errCount | Should Be 2
$fqeid | Should Be "GroupNotFound,Microsoft.PowerShell.Commands.RemoveLocalGroupCommand"
# confirm that the first group was removed
$sb = {
Get-LocalGroup "TestGroupRemove1"
}
VerifyFailingTest $sb "GroupNotFound,Microsoft.PowerShell.Commands.GetLocalGroupCommand"
}
}
}
finally {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}