-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Microsoft.PowerApps.Administration.PowerShell.Samples.psm1
2470 lines (2090 loc) · 99.4 KB
/
Microsoft.PowerApps.Administration.PowerShell.Samples.psm1
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
# Sample functions
function AllEnvironmentsPolicyTests
{
param
(
[Parameter(Mandatory = $true)]
[string]$EnvironmentDisplayName,
[Parameter(Mandatory = $false)]
[string]$PolicyDisplayName = "Test policy for AllEnvironments"
)
process
{
try
{
Write-Host "`r`nAllEnvironmentsPolicyTests started`r`n"
CleanV1TestPolicies
Write-Host "Create a new policy for AllEnvironments"
$response = New-DlpPolicy -DisplayName $PolicyDisplayName -EnvironmentType "AllEnvironments"
StringsAreEqual -Expect $PolicyDisplayName -Actual $response.displayName
Write-Host "Add connector groups"
$response.connectorGroups = CreateConnectorGroups
$response = Set-DlpPolicy -PolicyName $response.name -UpdatedPolicy $response
IsNotNull($response.Internal.connectorGroups | Where-Object { $_.classification -eq 'General' })
IsNotNull($response.Internal.connectorGroups | Where-Object { $_.classification -eq 'Confidential' })
IsNotNull($response.Internal.connectorGroups | Where-Object { $_.classification -eq 'Blocked' })
ListGetUpdateRemovePolicy -PolicyDisplayName $PolicyDisplayName -EnvironmentType "AllEnvironments"
Write-Host "Add the same connector to all groups test"
$newPolicy = [pscustomobject]@{
displayName = $PolicyDisplayName
defaultConnectorsClassification = "General"
connectorGroups = CreateConnectorGroupsForDuplicatedConnector
environmentType = $EnvironmentType
environments = @()
etag = $null
}
$response = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect "Bad Request" -Actual $response.StatusDescription
Write-Host "`r`nAllEnvironmentsPolicyTests completed"
} catch {
WriteStack
}
}
}
function ChangeOnlyToExceptEnvironmentsPolicyTests
{
param
(
[Parameter(Mandatory = $true)]
[string]$EnvironmentDisplayName,
[Parameter(Mandatory = $false)]
[string]$PolicyDisplayName = "Test policy for OnlyEnvironments"
)
process
{
try
{
Write-Host "`r`nChangeOnlyToExceptEnvironmentsPolicyTests started`r`n"
# remove the test environments
CleanTestEnvironments -EnvironmentDisplayName $EnvironmentDisplayName
CleanV1TestPolicies
$environment = CreateEnvironmentWithoutCDSDatabase -EnvironmentDisplayName $EnvironmentDisplayName
if($environment -eq $null)
{
throw "CreateEnvironment failed."
}
$newPolicy = CreatePolicyObject -EnvironmentType "OnlyEnvironments" -PolicyDisplayName $PolicyDisplayName
$environment = [pscustomobject]@{
id = "/providers/admin/environment"
name = $environment.EnvironmentName
type = "/providers/dummyEnvironments"
}
$newPolicy.environments += $environment
Write-Host "Create a new policy for OnlyEnvironments"
$response = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $PolicyDisplayName -Actual $response.displayName
$ExceptEnvironmentsPolicyDisplayName = "ExceptEnvironments policy"
$newPolicy = CreatePolicyObject -EnvironmentType "ExceptEnvironments" -PolicyDisplayName $ExceptEnvironmentsPolicyDisplayName
$environment = [pscustomobject]@{
id = "/providers/admin/environment"
name = $environment.EnvironmentName
type = "/providers/dummyEnvironments"
}
$newPolicy.environments += $environment
Write-Host "Create a new policy for ExceptEnvironments by using the same environment for OnlyEnvironments"
$exceptEnvironmentsResponse = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect "Bad Request" -Actual $exceptEnvironmentsResponse.StatusDescription
Write-Host "Change from EnvironmentType from Only to Except"
$updatedPolicy = [pscustomobject]$response
$updatedPolicy.environmentType = "ExceptEnvironments"
$response = Set-DlpPolicy -PolicyName $updatedPolicy.name -UpdatedPolicy $updatedPolicy
StringsAreEqual -Expect "ExceptEnvironments" -Actual $response.Internal.environmentType
Write-Host "Remove the test policy"
$response = CheckHttpResponse(Remove-DlpPolicy -PolicyName $updatedPolicy.name)
StringsAreEqual -Expect "OK" -Actual $response.Description
#Change EnvironmentType from Only to Except
Write-Host "`r`nChangeOnlyToExceptEnvironmentsPolicyTests completed"
} catch {
WriteStack
}
}
}
function SingleEnvironmentPolicyTests
{
param
(
[Parameter(Mandatory = $true)]
[string]$EnvironmentDisplayName,
[Parameter(Mandatory = $false)]
[string]$PolicyDisplayName = "Test policy for SingleEnvironment",
[Parameter(Mandatory = $false)]
[string]$EndPoint,
[Parameter(Mandatory = $false)]
[string]$TenantAdminName,
[Parameter(Mandatory = $false)]
[string]$TenantAdminPassword,
[Parameter(Mandatory = $false)]
[string]$EnvironmentAdminName,
[Parameter(Mandatory = $false)]
[string]$EnvironmentAdminPassword
)
process
{
try
{
Write-Host "`r`nSingleEnvironmentPolicyTests started`r`n"
# remove the test environments
CleanTestEnvironments -EnvironmentDisplayName $EnvironmentDisplayName
# remove the test policies
CleanV1TestPolicies
Write-Host "Change to a user EnvironmentAdmin"
$Password = ConvertTo-SecureString $EnvironmentAdminPassword -AsPlainText -Force
Add-PowerAppsAccount -Endpoint $EndPoint -Username $EnvironmentAdminName -Password $Password
$EnvironmentDisplayName = $EnvironmentDisplayName + " for NonAdmin"
$environment = CreateEnvironmentWithoutCDSDatabase -EnvironmentDisplayName $EnvironmentDisplayName
if($environment -eq $null)
{
throw "CreateEnvironment failed."
}
$newPolicy = CreatePolicyObject -EnvironmentType "SingleEnvironment" -PolicyDisplayName $PolicyDisplayName
$environment = [pscustomobject]@{
id = "/providers/Microsoft.BusinessAppPlatform/scopes/admin/environments/$($environment.EnvironmentName)"
name = $environment.EnvironmentName
type = "Microsoft.BusinessAppPlatform/scopes/environments"
}
$newPolicy.environments += $environment
Write-Host "Create a new policy for SingleEnvironment"
$response = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $PolicyDisplayName -Actual $response.displayName
ListGetUpdateRemovePolicy -PolicyDisplayName $PolicyDisplayName -EnvironmentType "SingleEnvironment"
Write-Host "Change user back to GlobalAdmin"
$Password = ConvertTo-SecureString $TenantAdminPassword -AsPlainText -Force
Add-PowerAppsAccount -Endpoint $EndPoint -Username $TenantAdminName -Password $Password
Write-Host "`r`nSingleEnvironmentPolicyTests completed"
} catch {
WriteStack
}
}
}
function CreateListMultiplePoliciesTests
{
param
(
[Parameter(Mandatory = $true)]
[string]$EnvironmentDisplayName
)
process
{
try
{
Write-Host "`r`nCreateListMultiplePoliciesTests started`r`n"
CleanV1TestPolicies
$AllEnvironmentsPolicyDisplayName = "AllEnvironments policy"
$newPolicy = CreatePolicyObject -EnvironmentType "AllEnvironments" -PolicyDisplayName $AllEnvironmentsPolicyDisplayName
Write-Host "Create a new policy for AllEnvironments"
$response = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $AllEnvironmentsPolicyDisplayName -Actual $response.displayName
$OnlyEnvironmentsPolicyDisplayName = "OnlyEnvironments policy"
$newPolicy = CreatePolicyObject -EnvironmentType "OnlyEnvironments" -PolicyDisplayName $OnlyEnvironmentsPolicyDisplayName
$environmentName = New-Guid
$environment = [pscustomobject]@{
id = "/providers/admin/environment"
name = $environmentName
type = "/providers/dummyEnvironments"
}
$newPolicy.environments += $environment
Write-Host "Create a new policy for OnlyEnvironments"
$response = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $OnlyEnvironmentsPolicyDisplayName -Actual $response.displayName
$ExceptEnvironmentsPolicyDisplayName = "ExceptEnvironments policy"
$newPolicy = CreatePolicyObject -EnvironmentType "ExceptEnvironments" -PolicyDisplayName $ExceptEnvironmentsPolicyDisplayName
$environmentName = New-Guid
$environment = [pscustomobject]@{
id = "/providers/admin/environment"
name = $environmentName
type = "/providers/dummyEnvironments"
}
$newPolicy.environments += $environment
Write-Host "Create a new policy for ExceptEnvironments"
$response = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $ExceptEnvironmentsPolicyDisplayName -Actual $response.displayName
$policies = Get-DlpPolicy
Write-Host "Check and remove all created policies"
foreach ($policy in $Policies.Value)
{
if ($policy.displayName -eq $AllEnvironmentsPolicyDisplayName -or
$policy.displayName -eq $OnlyEnvironmentsPolicyDisplayName -or
$policy.displayName -eq $ExceptEnvironmentsPolicyDisplayName)
{
$response = CheckHttpResponse(Remove-DlpPolicy -PolicyName $policy.name)
StringsAreEqual -Expect "OK" -Actual $response.Description
}
else
{
throw "CreateListMultiplePoliciesTests fails ($policy.displayName)"
}
}
Write-Host "`r`nCreateListMultiplePoliciesTests completed"
} catch {
WriteStack
}
}
}
function MoveConnectorCrossGroupsTests
{
param
(
[Parameter(Mandatory = $false)]
[string]$PolicyDisplayName = "Test policy for moving connectors"
)
process
{
try
{
Write-Host "`r`nMoveConnectorCrossGroupsTests started`r`n"
CleanV1TestPolicies
$newPolicy = CreatePolicyObject -EnvironmentType "AllEnvironments" -PolicyDisplayName $PolicyDisplayName
$response = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $PolicyDisplayName -Actual $response.displayName
$confidentialGroup = $response.connectorGroups | Where-Object { $_.classification -eq 'Confidential' }
$blockedGroup = $response.connectorGroups | Where-Object { $_.classification -eq 'Blocked' }
IntAreEqual -Expect 3 -Actual $confidentialGroup.connectors.Count
IntAreEqual -Expect 1 -Actual $blockedGroup.connectors.Count
Write-Host "Move a connector from Confidential group to Blocked group."
$connector = $confidentialGroup.connectors[0]
# remove Confidential group from connectorGroups
$response.connectorGroups = $response.connectorGroups -ne $confidentialGroup
# remove Blocked group from connectorGroups
$response.connectorGroups = $response.connectorGroups -ne $blockedGroup
# remove the connector from Confidential group
$confidentialConnectors = $confidentialGroup.connectors -ne $connector
$confidentialGroup.connectors = $confidentialConnectors
# add the connector to Blocked group
$blockedGroup.connectors += $connector
# add Confidential group back
$response.connectorGroups += $confidentialGroup
# add blocked group back
$response.connectorGroups += $blockedGroup
# update policy
$response = Set-DlpPolicy -PolicyName $response.name -UpdatedPolicy $response
$confidentialGroup = $response.Internal.connectorGroups | Where-Object { $_.classification -eq 'Confidential' }
$blockedGroup = $response.Internal.connectorGroups | Where-Object { $_.classification -eq 'Blocked' }
# Confidential group number of connectors changed from 3 to 2
IntAreEqual -Expect 2 -Actual $confidentialGroup.connectors.Count
# Blocked group number of connectors changed from 1 to 2
IntAreEqual -Expect 2 -Actual $blockedGroup.connectors.Count
Write-Host "`r`nMoveConnectorCrossGroupsTests completed"
} catch {
WriteStack
}
}
}
function OldAPIToNewAPICompatibilityTests
{
param
(
[Parameter(Mandatory = $true)]
[string]$EnvironmentDisplayName
)
process
{
try
{
Write-Host "`r`nOldAPIToNewAPICompatibilityTests started`r`n"
# remove the test environments
CleanTestEnvironments -EnvironmentDisplayName $EnvironmentDisplayName
CleanV1TestPolicies
$AllEnvironmentsPolicyDisplayName = "AllEnvironments policy"
$newPolicy = CreatePolicyObject -EnvironmentType "AllEnvironments" -PolicyDisplayName $AllEnvironmentsPolicyDisplayName
Write-Host "Create a new policy for AllEnvironments"
$newCreatedPolicy = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $AllEnvironmentsPolicyDisplayName -Actual $newCreatedPolicy.displayName
# define customer connector for old API
$TenantPolicyTestDisplayName = "TenantPolicyDemo"
$EnvironmentPolicyDisplayName = "EnvironmentPolicyDemo"
$NonBusinessConnectorName = "shared_graph-2dapi-2dtest-5f490bdb62ac165ca8-5fbf5c83266b2adfb5"
$NonBusinessConnectorId = "/providers/Microsoft.PowerApps/scopes/admin/environments/Default-bde1d79a-4825-4883-a114-b4a801feaf16/apis/shared_graph-2dapi-2dtest-5f490bdb62ac165ca8-5fbf5c83266b2adfb5"
$NonBusinessConnectorType = "Microsoft.PowerApps/apis"
$BusinessConnectorName = "shared_graph-2dapi-2dtest-5f490bdb62ac165ca8-5fbf5c83266b2adfb5"
$BusinessConnectorId = "/providers/Microsoft.PowerApps/scopes/admin/environments/Default-bde1d79a-4825-4883-a114-b4a801feaf16/apis/shared_graph-2dapi-2dtest-5f490bdb62ac165ca8-5fbf5c83266b2adfb5"
$BusinessConnectorType = "Microsoft.PowerApps/apis"
# create test tenant policy
Write-Host "Create an old tenant policy"
$tenantPolicy = New-AdminDlpPolicy -DisplayName $TenantPolicyTestDisplayName
StringsAreEqual -Expect $TenantPolicyTestDisplayName -Actual $tenantPolicy.DisplayName
<# Update test tenant policy #>
$tenantPolicyResponse = Set-AdminDlpPolicy -PolicyName $tenantPolicy.PolicyName -SetNonBusinessDataGroupState "Block"
StringsAreEqual -Expect "Block" -Actual $tenantPolicyResponse.Internal.properties.definition.rules.apiGroupRule.actions.blockAction.type
$apiGroupRule = ConvertTo-Json $tenantPolicyResponse.Internal.properties.definition.rules.apiGroupRule
Write-Verbose "Test tenant policy apiGroupRule added:`r`n$apiGroupRule"
# Add customer connector to BusinessData group of tenant policy
$response = CheckHttpResponse(Add-CustomConnectorToPolicy -PolicyName $tenantPolicy.PolicyName -ConnectorName $BusinessConnectorName -ConnectorId $BusinessConnectorId -ConnectorType $BusinessConnectorType -GroupName hbi)
$content = ConvertFrom-Json $response.Internal.Content
StringsAreEqual -Expect $BusinessConnectorName -Actual $content.properties.definition.apiGroups.hbi.apis[0].name
# Add customer connector to Non-BusinessData group of tenant policy
$response = CheckHttpResponse(Add-CustomConnectorToPolicy -PolicyName $tenantPolicy.PolicyName -ConnectorName $NonBusinessConnectorName -ConnectorId $NonBusinessConnectorId -ConnectorType $NonBusinessConnectorType -GroupName lbi)
$content = ConvertFrom-Json $response.Internal.Content
StringsAreEqual -Expect $NonBusinessConnectorName -Actual $content.properties.definition.apiGroups.lbi.apis[0].name
$updatedTenantPolicy = $content
# create test environment policy
Write-Host "Create an old environment policy"
$environment = CreateEnvironmentWithoutCDSDatabase -EnvironmentDisplayName $EnvironmentDisplayName
if($environment -eq $null)
{
throw "CreateEnvironment failed."
}
$envirnmentPolicy = New-AdminDlpPolicy -DisplayName $EnvironmentPolicyDisplayName -EnvironmentName $environment.EnvironmentName
StringsAreEqual -Expect $EnvironmentPolicyDisplayName -Actual $envirnmentPolicy.DisplayName
<# update test environment policy #>
$envirnmentPolicyResponse = Set-AdminDlpPolicy -PolicyName $envirnmentPolicy.PolicyName -EnvironmentName $environment.EnvironmentName -SetNonBusinessDataGroupState "Block"
StringsAreEqual -Expect "Block" -Actual $envirnmentPolicyResponse.Internal.properties.definition.rules.apiGroupRule.actions.blockAction.type
$apiGroupRule = ConvertTo-Json $envirnmentPolicyResponse.Internal.properties.definition.rules.apiGroupRule
Write-Verbose "Test environment policy apiGroupRule added:`r`n$apiGroupRule"
# Add customer connector to BusinessData group of environment policy
$response = CheckHttpResponse(Add-CustomConnectorToPolicy -PolicyName $envirnmentPolicy.PolicyName -EnvironmentName $environment.EnvironmentName -ConnectorName $BusinessConnectorName -ConnectorId $BusinessConnectorId -ConnectorType $BusinessConnectorType -GroupName hbi)
$content = ConvertFrom-Json $response.Internal.Content
StringsAreEqual -Expect $BusinessConnectorName -Actual $content.properties.definition.apiGroups.hbi.apis[0].name
# Add customer connector to Non-BusinessData group of environment policy
$response = CheckHttpResponse(Add-CustomConnectorToPolicy -PolicyName $envirnmentPolicy.PolicyName -EnvironmentName $environment.EnvironmentName -ConnectorName $NonBusinessConnectorName -ConnectorId $NonBusinessConnectorId -ConnectorType $NonBusinessConnectorType -GroupName lbi)
$content = ConvertFrom-Json $response.Internal.Content
StringsAreEqual -Expect $NonBusinessConnectorName -Actual $content.properties.definition.apiGroups.lbi.apis[0].name
$updatedEnvirnmentPolicy = $content
# using new API to list and remove policies
$policies = Get-DlpPolicy
Write-Host "Check and remove all created policies"
foreach ($policy in $Policies.Value)
{
$generalGroup = $policy.connectorGroups | Where-Object { $_.classification -eq 'General' }
$confidentialGroup = $policy.connectorGroups | Where-Object { $_.classification -eq 'Confidential' }
$blockedGroup = $policy.connectorGroups | Where-Object { $_.classification -eq 'Blocked' }
if ($policy.displayName -eq $TenantPolicyTestDisplayName)
{
StringsAreEqual -Expect $updatedTenantPolicy.properties.CreatedBy.displayName -Actual $policy.CreatedBy.displayName
StringsAreEqual -Expect $updatedTenantPolicy.properties.createdTime -Actual $policy.createdTime
StringsAreEqual -Expect $updatedTenantPolicy.properties.lastModifiedBy.displayName -Actual $policy.lastModifiedBy.displayName
StringsAreEqual -Expect $updatedTenantPolicy.properties.lastModifiedTime -Actual $policy.lastModifiedTime
# connector group check, lbi connector moves to blocked group, hbi connector moves to general group
StringsAreEqual -Expect $updatedTenantPolicy.properties.definition.apiGroups.lbi.apis[0].id -Actual $blockedGroup.connectors[0].id
StringsAreEqual -Expect $updatedTenantPolicy.properties.definition.apiGroups.lbi.apis[0].name -Actual $blockedGroup.connectors[0].name
StringsAreEqual -Expect $updatedTenantPolicy.properties.definition.apiGroups.lbi.apis[0].type -Actual $blockedGroup.connectors[0].type
StringsAreEqual -Expect $updatedTenantPolicy.properties.definition.apiGroups.hbi.apis[0].id -Actual $generalGroup.connectors[0].id
StringsAreEqual -Expect $updatedTenantPolicy.properties.definition.apiGroups.hbi.apis[0].name -Actual $generalGroup.connectors[0].name
StringsAreEqual -Expect $updatedTenantPolicy.properties.definition.apiGroups.hbi.apis[0].type -Actual $generalGroup.connectors[0].type
StringsAreEqual -Expect "Microsoft.BusinessAppPlatform/scopes/apiPolicies" -Actual $tenantPolicy.Type
StringsAreEqual -Expect "AllEnvironments" -Actual $policy.environmentType
# update policy displayName
$updatedDisplayName = "Updated old API test policy"
$policy.displayName = $updatedDisplayName
$response = Set-DlpPolicy -PolicyName $policy.name -UpdatedPolicy $policy
StringsAreEqual -Expect $updatedDisplayName -Actual $response.Internal.displayName
}
elseif ($policy.displayName -eq $AllEnvironmentsPolicyDisplayName)
{
StringsAreEqual -Expect $newCreatedPolicy.CreatedBy.displayName -Actual $policy.CreatedBy.displayName
StringsAreEqual -Expect $newCreatedPolicy.createdTime -Actual $policy.createdTime
StringsAreEqual -Expect $newCreatedPolicy.lastModifiedBy.displayName -Actual $policy.lastModifiedBy.displayName
StringsAreEqual -Expect $newCreatedPolicy.lastModifiedTime -Actual $policy.lastModifiedTime
StringsAreEqual -Expect $newCreatedPolicy.etag -Actual $policy.etag
$newCreatedGeneralGroup = $newCreatedPolicy.connectorGroups | Where-Object { $_.classification -eq 'General' }
$newCreatedConfidentialGroup = $newCreatedPolicy.connectorGroups | Where-Object { $_.classification -eq 'Confidential' }
$newCreatedBlockedGroup = $newCreatedPolicy.connectorGroups | Where-Object { $_.classification -eq 'Blocked' }
StringsAreEqual -Expect $newCreatedGeneralGroup.connectors[0].id -Actual $generalGroup.connectors[0].id
StringsAreEqual -Expect $newCreatedConfidentialGroup.connectors[0].id -Actual $confidentialGroup.connectors[0].id
StringsAreEqual -Expect $newCreatedConfidentialGroup.connectors[1].id -Actual $confidentialGroup.connectors[1].id
StringsAreEqual -Expect $newCreatedBlockedGroup.connectors[0].id -Actual $blockedGroup.connectors[0].id
$upatedDisplayName = "Updated new API test policy"
Write-Host "Upate policy to $upatedDisplayName"
$updatedPolicy = [pscustomobject]$policy
$updatedPolicy.displayName = $upatedDisplayName
$response = Set-DlpPolicy -PolicyName $updatedPolicy.name -UpdatedPolicy $updatedPolicy
StringsAreEqual -Expect $upatedDisplayName -Actual $response.Internal.displayName
}
elseif ($policy.displayName -eq $EnvironmentPolicyDisplayName)
{
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.CreatedBy.displayName -Actual $policy.CreatedBy.displayName
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.createdTime -Actual $policy.createdTime
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.lastModifiedBy.displayName -Actual $policy.lastModifiedBy.displayName
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.lastModifiedTime -Actual $policy.lastModifiedTime
StringsAreEqual -Expect $envirnmentPolicy.Environments[0].name -Actual $policy.Environments[0].name
StringsAreEqual -Expect $envirnmentPolicy.Environments[0].id -Actual $policy.Environments[0].id
StringsAreEqual -Expect $envirnmentPolicy.Environments[0].type -Actual $policy.Environments[0].type
# connector group check, lbi connector moves to blocked group, hbi connector moves to general group
StringsAreEqual -Expect "Blocked" -Actual $blockedGroup.classification
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.definition.apiGroups.lbi.apis[0].id -Actual $blockedGroup.connectors[0].id
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.definition.apiGroups.lbi.apis[0].name -Actual $blockedGroup.connectors[0].name
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.definition.apiGroups.lbi.apis[0].type -Actual $blockedGroup.connectors[0].type
StringsAreEqual -Expect "General" -Actual $generalGroup.classification
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.definition.apiGroups.hbi.apis[0].id -Actual $generalGroup.connectors[0].id
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.definition.apiGroups.hbi.apis[0].name -Actual $generalGroup.connectors[0].name
StringsAreEqual -Expect $updatedEnvirnmentPolicy.properties.definition.apiGroups.hbi.apis[0].type -Actual $generalGroup.connectors[0].type
StringsAreEqual -Expect "Microsoft.BusinessAppPlatform/scopes/environments/apiPolicies" -Actual $envirnmentPolicy.Type
StringsAreEqual -Expect "SingleEnvironment" -Actual $policy.environmentType
# update policy displayName
$updatedDisplayName = "Updated old API environment test policy"
$policy.displayName = $updatedDisplayName
$response = Set-DlpPolicy -PolicyName $policy.name -UpdatedPolicy $policy
StringsAreEqual -Expect $updatedDisplayName -Actual $response.Internal.displayName
}
else
{
throw "CreateListMultiplePoliciesTests fails ($policy.displayName)"
}
}
CleanV1TestPolicies
Write-Host "`r`nOldAPIToNewAPICompatibilityTests completed"
} catch {
WriteStack
}
}
}
function NewAPIToOldAPICompatibilityTests
{
param
(
[Parameter(Mandatory = $true)]
[string]$EnvironmentDisplayName
)
process
{
try
{
Write-Host "`r`nNewAPIToOldAPICompatibilityTests started`r`n"
# remove the test environments
CleanTestEnvironments -EnvironmentDisplayName $EnvironmentDisplayName
CleanV1TestPolicies
$AllEnvironmentsPolicyDisplayName = "AllEnvironments policy"
$newPolicy = CreatePolicyObject -EnvironmentType "AllEnvironments" -PolicyDisplayName $AllEnvironmentsPolicyDisplayName
Write-Host "Create a new policy for AllEnvironments"
$allEnvironmentsResponse = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $AllEnvironmentsPolicyDisplayName -Actual $allEnvironmentsResponse.displayName
# create test environment
Write-Host "Create a test environment"
$environment = CreateEnvironmentWithoutCDSDatabase -EnvironmentDisplayName $EnvironmentDisplayName
if($environment -eq $null)
{
throw "CreateEnvironment failed."
}
$OnlyEnvironmentsPolicyDisplayName = "OnlyEnvironments policy"
$newPolicy = CreatePolicyObject -EnvironmentType "OnlyEnvironments" -PolicyDisplayName $OnlyEnvironmentsPolicyDisplayName
$environment = [pscustomobject]@{
id = "/providers/admin/environment"
name = $environment.EnvironmentName
type = "/providers/dummyEnvironments"
}
$newPolicy.environments += $environment
Write-Host "Create a new policy for OnlyEnvironments"
$onlyEnvironmentsResponse = New-DlpPolicy -NewPolicy $newPolicy
StringsAreEqual -Expect $OnlyEnvironmentsPolicyDisplayName -Actual $onlyEnvironmentsResponse.displayName
# using old API to list and remove policies
$policies = Get-AdminDlpPolicy
Write-Host "Check and remove all created policies"
foreach ($policy in $Policies)
{
if ($policy.displayName -eq $AllEnvironmentsPolicyDisplayName)
{
PolicyCheck -NewPolicy $allEnvironmentsResponse -OldPolicy $policy -EnvironmentType "AllEnvironments"
# update test tenant policy
$policyResponse = Set-AdminDlpPolicy -PolicyName $policy.PolicyName -SetNonBusinessDataGroupState "Block"
StringsAreEqual -Expect "Block" -Actual $policyResponse.Internal.properties.definition.rules.apiGroupRule.actions.blockAction.type
}
elseif ($policy.displayName -eq $OnlyEnvironmentsPolicyDisplayName)
{
PolicyCheck -NewPolicy $onlyEnvironmentsResponse -OldPolicy $policy -EnvironmentType "OnlyEnvironments"
StringsAreEqual -Expect $onlyEnvironmentsResponse.Environments[0].id -Actual $policy.Environments[0].id
StringsAreEqual -Expect $onlyEnvironmentsResponse.Environments[0].name -Actual $policy.Environments[0].name
StringsAreEqual -Expect $onlyEnvironmentsResponse.Environments[0].type -Actual $policy.Environments[0].type
StringsAreEqual -Expect $onlyEnvironmentsResponse.Environments[0].id -Actual $policy.Constraints.environmentFilter1.parameters.environments[0].id
StringsAreEqual -Expect $onlyEnvironmentsResponse.Environments[0].name -Actual $policy.Constraints.environmentFilter1.parameters.environments[0].name
StringsAreEqual -Expect $onlyEnvironmentsResponse.Environments[0].type -Actual $policy.Constraints.environmentFilter1.parameters.environments[0].type
}
else
{
throw "NewAPIToOldAPICompatibilityTests fails ($policy.properties.displayName)"
}
}
# use old API to remove new policies
CleanTestPolicies
Write-Host "`r`nOldAPIToNewAPICompatibilityTests completed"
} catch {
WriteStack
}
}
}
function DLPPolicyConnectorActionControlCrud
{
param
(
[Parameter(Mandatory = $false)]
[string]$TenantPolicyTestDisplayName = "TenantPolicyDemo"
)
process
{
$connectorId = "/providers/Microsoft.PowerApps/apis/shared_msnweather"
$connectorName = "shared_msnweather"
$desiredActionBehavior = "Block"
$desiredDefaultBehavior = "Allow"
$tenantId = $global:currentSession.tenantId;
Write-Host "Get connector shared_msnweather actions"
$connectorActions = Get-AdminPowerAppConnectorAction -ConnectorName $connectorName
$connectorAction = $connectorActions[0]
Write-Host "Get all policies"
$policies = Get-DlpPolicy
if ($policies -ne $null -and $policies.value -ne $null)
{
foreach ($policy in $policies.value)
{
if ($policy.displayName -eq $TenantPolicyTestDisplayName)
{
$tenantPolicy = $policy
break
}
}
}
if ($tenantPolicy -eq $null)
{
Write-Host "Create test tenant policy."
$tenantPolicy = New-DlpPolicy -DisplayName $TenantPolicyTestDisplayName -EnvironmentType "AllEnvironments"
}
Write-Host "Get connector configuration."
$policyConnectorConfigurations = Get-PowerAppDlpPolicyConnectorConfigurations -TenantId $tenantId -PolicyName $tenantPolicy.Name
$connectorConfigurationsAlreadyExists = $false
if ($policyConnectorConfigurations -ne $null)
{
$connectorConfigurationsAlreadyExists = $true
}
else
{
$policyConnectorConfigurations = New-Object -TypeName PSObject
}
if ($policyConnectorConfigurations.connectorActionConfigurations -eq $null)
{
$policyConnectorConfigurations | Add-Member -PassThru -MemberType NoteProperty -Name connectorActionConfigurations -Value @()
}
Write-Host "Loop through policy connector action configurations and find the connector based on connector Id."
$msnWeatherConnectorActionConfigurations = $null
foreach ($connectorConfiguration in $policyConnectorConfigurations.connectorActionConfigurations)
{
if ($connectorConfiguration.connectorId -eq $connectorId)
{
$msnWeatherConnectorActionConfigurations = $connectorConfiguration
break
}
}
Write-Host "If the connector action configuration does not exist, add the connector action configuration."
if ($msnWeatherConnectorActionConfigurations -eq $null)
{
$msnWeatherConnectorActionConfigurations = [pscustomobject]@{
connectorId = $connectorId
actionRules = @()
defaultConnectorActionRuleBehavior = $desiredDefaultBehavior
}
$policyConnectorConfigurations.connectorActionConfigurations += $msnWeatherConnectorActionConfigurations
}
Write-Host "Loop through policy connector action configurations action rules and find the action rule based on connector action."
$msnWeatherConnectorActionRule = $null
foreach ($actionRule in $msnWeatherConnectorActionConfigurations.actionRules)
{
if ($actionRule.ActionId -eq $connectorAction.Id)
{
$msnWeatherConnectorActionRule = $actionRule
break
}
}
Write-Host "If the action rule does not exist, add the action rule."
if ($msnWeatherConnectorActionRule -eq $null)
{
$msnWeatherConnectorActionRule = [pscustomobject]@{
ActionId = $connectorAction.Id
behavior = $desiredActionBehavior
}
$msnWeatherConnectorActionConfigurations.actionRules += $msnWeatherConnectorActionRule
}
if ($connectorConfigurationsAlreadyExists)
{
Write-Host "Update the policy connector configurations."
Set-PowerAppDlpPolicyConnectorConfigurations -PolicyName $tenantPolicy.Name -UpdatedConnectorConfigurations $policyConnectorConfigurations -TenantId $tenantId | Out-Null
$removeConnectorConfigration = $true
}
else
{
Write-Host "Create a new dlp policy connector configurations."
New-PowerAppDlpPolicyConnectorConfigurations -NewDlpPolicyConnectorConfigurations $policyConnectorConfigurations -TenantId $tenantId -PolicyName $tenantPolicy.Name | Out-Null
$removeConnectorConfigration = $false
}
if ($removeConnectorConfigration)
{
Write-Host "Remove the policy connector configurations."
Remove-PowerAppDlpPolicyConnectorConfigurations -TenantId $tenantId -PolicyName $tenantPolicy.Name | Out-Null
}
}
}
function DLPPolicyConnectorEndpointControlCrud
{
param
(
[Parameter(Mandatory = $false)]
[string]$TenantPolicyTestDisplayName = "TenantPolicyDemo"
)
process
{
$connectorId = "/providers/Microsoft.PowerApps/apis/shared_sql"
$connectorName = "shared_sql"
$initialEndpoint = "www.a.*.com"
$updatedEndPoint = "www.b.*.com"
$tenantId = $global:currentSession.tenantId;
Write-Host "Get all policies"
$policies = Get-DlpPolicy
if ($policies -ne $null -and $policies.value -ne $null)
{
foreach ($policy in $policies.value)
{
if ($policy.displayName -eq $TenantPolicyTestDisplayName)
{
$tenantPolicy = $policy
break
}
}
}
if ($tenantPolicy -eq $null)
{
Write-Host "Create test tenant policy."
$tenantPolicy = New-DlpPolicy -DisplayName $TenantPolicyTestDisplayName -EnvironmentType "AllEnvironments"
}
Write-Host "Get connector configuration."
$policyConnectorConfigurations = Get-PowerAppDlpPolicyConnectorConfigurations -TenantId $tenantId -PolicyName $tenantPolicy.Name
$connectorConfigurationsAlreadyExists = $false
if ($policyConnectorConfigurations -ne $null)
{
$connectorConfigurationsAlreadyExists = $true
}
else
{
$policyConnectorConfigurations = New-Object -TypeName PSObject
}
if ($policyConnectorConfigurations.endpointConfigurations -eq $null)
{
$policyConnectorConfigurations | Add-Member -PassThru -MemberType NoteProperty -Name endpointConfigurations -Value @()
}
Write-Host "Loop through policy connector endpoint configurations and find the connector configuration based on connector Id."
$sqlConnectorEndpointConfigurations = $null
foreach ($connectorConfiguration in $policyConnectorConfigurations.endpointConfigurations)
{
if ($connectorConfiguration.connectorId -eq $connectorId)
{
$sqlConnectorEndpointConfigurations = $connectorConfiguration
break
}
}
Write-Host "If the connector endpoint configuration does not exist, add the connector endpoint configuration."
if ($sqlConnectorEndpointConfigurations -eq $null)
{
$sqlConnectorEndpointConfigurations = [pscustomobject]@{
connectorId = $connectorId
endpointRules = @()
}
$policyConnectorConfigurations.endpointConfigurations += $sqlConnectorEndpointConfigurations
}
Write-Host "Loop through policy connector endpoint configurations endpoint rules and find the endpoint rule based on the endpoint."
$endpointUpdated = $false
foreach ($endpointRule in $sqlConnectorEndpointConfigurations.endpointRules)
{
if ($endpointRule.endPoint -eq $initialEndpoint)
{
# Update the endpoint rule in the 3nd run.
$endpointRule.endPoint = $updatedEndPoint
$endpointUpdated = $true
break
}
}
Write-Host "If the endpoint rule does not exist, add the endpoint rule."
if ($sqlConnectorEndpointConfigurations.endpointRules.Count -eq 0)
{
# Add the last endpoint rule in the first run.
$lastEndpointRule = [pscustomobject]@{
order = 1
behavior = "Deny"
endPoint = "*"
}
$sqlConnectorEndpointConfigurations.endpointRules += $lastEndpointRule
}
else
{
# Increase the last endpoint rule order
$lastOrder = $sqlConnectorEndpointConfigurations.endpointRules[$sqlConnectorEndpointConfigurations.endpointRules.Count - 1].order
$sqlConnectorEndpointConfigurations.endpointRules[$sqlConnectorEndpointConfigurations.endpointRules.Count - 1].order = $lastOrder + 1
# Add a new endpoint rule
$newEndPointRule = [pscustomobject]@{
order = $lastOrder
behavior = "Allow"
endPoint = $initialEndpoint
}
$sqlConnectorEndpointConfigurations.endpointRules += $newEndPointRule
# Sort endpoint rules by order in ascending
$sqlConnectorEndpointConfigurations.endpointRules = $sqlConnectorEndpointConfigurations.endpointRules | Sort-Object -Property order
# After the 3nd run, there are 3 endpoint rules.
#[DBG]: PS C:\>> $sqlConnectorEndpointConfigurations.endpointRules
#
#order behavior endPoint
#----- -------- --------
# 1 Allow www.b.*.com
# 2 Allow www.a.*.com
# 3 Deny *
}
$removeConnectorConfigration = $false
if ($connectorConfigurationsAlreadyExists)
{
Write-Host "Update the policy connector configurations."
Set-PowerAppDlpPolicyConnectorConfigurations -PolicyName $tenantPolicy.Name -UpdatedConnectorConfigurations $policyConnectorConfigurations -TenantId $tenantId | Out-Null
if ($endpointUpdated)
{
$removeConnectorConfigration = $true
}
}
else
{
Write-Host "Create a new dlp policy connector configurations."
New-PowerAppDlpPolicyConnectorConfigurations -NewDlpPolicyConnectorConfigurations $policyConnectorConfigurations -TenantId $tenantId -PolicyName $tenantPolicy.Name | Out-Null
}
if ($removeConnectorConfigration)
{
Write-Host "Remove the policy connector configurations."
Remove-PowerAppDlpPolicyConnectorConfigurations -TenantId $tenantId -PolicyName $tenantPolicy.Name | Out-Null
}
}
}
function Add-ConnectorToBusinessDataGroupSample
{
<#
.SYNOPSIS
Sets connector to the business data group of data loss policy.
.DESCRIPTION
The code is changed to using new DLP API and set connector to the business data group depending on parameters.
.PARAMETER PolicyName
The PolicyName's identifier.
.PARAMETER ConnectorName
The Connector's identifier.
.EXAMPLE
Add-ConnectorToBusinessDataGroup -PolicyName e25a94b2-3111-468e-9125-3d3db3938f13 -ConnectorName shared_office365users
Sets the connector to Confidential group of policyname e25a94b2-3111-468e-9125-3d3db3938f13.
#>
param
(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string]$PolicyName,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string]$ConnectorName
)
process
{
$policy = Get-DlpPolicy -PolicyName $PolicyName
$confidentialGroup = $policy.connectorGroups | Where-Object { $_.classification -eq 'Confidential' }
$connectorInConfidential = $confidentialGroup.connectors | where { $_.name -eq $ConnectorName }
if($connectorInConfidential -ne $null)
{
Write-Error "Connector already exists in Confidential group"
return $null
}
$connector = Get-PowerAppConnector -EnvironmentName $policy.environments[0].name -ConnectorName $ConnectorName `
| %{ New-Object -TypeName PSObject -Property @{ id = $_.connectorId; name = ($_.connectorId -split "/apis/")[1]; type = $_.internal.type } }
if($connector -eq $null)
{
Write-Error "No connector with specified name found"
return $null
}
#Add the connector to the confidential group of policy
$confidentialGroup.connectors += $connector
#remove the connector from General group if exist
$generalGroup = $policy.connectorGroups | Where-Object { $_.classification -eq 'General' }
$generalConnectorsWithoutProvidedConnector = $generalGroup.connectors | Where-Object { $_.id -ne $connector.id }
if ($generalConnectorsWithoutProvidedConnector -eq $null)
{
$generalConnectorsWithoutProvidedConnector = @()
}
$generalGroup.connectors = [Array]$generalConnectorsWithoutProvidedConnector
#Update policy
Set-DlpPolicy -PolicyName $policy.name -UpdatedPolicy $policy
}
}
function Remove-ConnectorFromBusinessDataGroupSample
{
<#
.SYNOPSIS
Removes connector from the business data group of data loss policy.
.DESCRIPTION
The Remove-ConnectorFromBusinessDataGroup removes connector from the business data group of DLP depending on parameters.
.PARAMETER PolicyName
The PolicyName's identifier.
.PARAMETER ConnectorName
The Connector's identifier.
.EXAMPLE
Remove-ConnectorFromBusinessDataGroup -PolicyName e25a94b2-3111-468e-9125-3d3db3938f13 -ConnectorName shared_office365users
Removes the connector from BusinessData group of policyname e25a94b2-3111-468e-9125-3d3db3938f13.
#>
param
(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string]$PolicyName,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string]$ConnectorName
)
process
{
$policy = Get-DlpPolicy -PolicyName $PolicyName
$generalGroup = $policy.connectorGroups | Where-Object { $_.classification -eq 'General' }
$connectorInGeneral = $generalGroup.connectors | where { $_.name -eq $ConnectorName }
if($connectorInGeneral -ne $null)
{
Write-Error "Connector already exists in General group"
return $null
}
$connector = Get-PowerAppConnector -EnvironmentName $policy.environments[0].name -ConnectorName $ConnectorName `
| %{ New-Object -TypeName PSObject -Property @{ id = $_.connectorId; name = ($_.connectorId -split "/apis/")[1]; type = $_.internal.type } }
if($connector -eq $null)
{
Write-Error "No connector with specified name found"
return $null
}
#Add the connector to the General group of the policy
$generalGroup.connectors += $connector