1+ # ################################################
2+ # HelloID-Conn-Prov-Target-ActiveDirectory-ImportSubPermission
3+ # PowerShell V2
4+ # ################################################
5+
6+ # Enable TLS1.2
7+ [System.Net.ServicePointManager ]::SecurityProtocol = [System.Net.ServicePointManager ]::SecurityProtocol -bor [System.Net.SecurityProtocolType ]::Tls12
8+
9+ try {
10+ Write-Information ' Starting target sub-permissions import '
11+
12+ # Configure, must be the same as the values used in retrieve permissions
13+ $permissionReference = ' dep'
14+ $permissionDisplayName = ' Department'
15+
16+ $filter = " Description -like 'department*'"
17+ # If all groups needs to be queried
18+ # $filter = '*'
19+
20+ # $searchOUs = @("OU=HelloID,OU=Security Groups,DC=enyoi,DC=org","OU=HelloID,OU=Other Groups,DC=enyoi,DC=org")
21+ # If all OUs needs to be queried
22+ $searchOUs = @ (" " )
23+
24+ $actionMessage = " getting primary domain controller"
25+ if ([string ]::IsNullOrEmpty($actionContext.Configuration.fixedDomainController )) {
26+ try {
27+ $pdc = (Get-ADForest | Select-Object - ExpandProperty RootDomain | Get-ADDomain | Select-Object - Property PDCEmulator).PDCEmulator
28+ }
29+ catch {
30+ Write-Warning (" PDC Lookup Error: {0}" -f $_.Exception.InnerException.Message )
31+ Write-Warning " Retrying PDC Lookup"
32+ $pdc = (Get-ADForest | Select-Object - ExpandProperty RootDomain | Get-ADDomain | Select-Object - Property PDCEmulator).PDCEmulator
33+ }
34+ }
35+ else {
36+ Write-Information " A fixed domain controller is configured [$ ( $actionContext.Configuration.fixedDomainController ) ]"
37+ $pdc = $ ($actionContext.Configuration.fixedDomainController )
38+ }
39+
40+ $actionMessage = " querying groups"
41+ $properties = @ (' ObjectGUID' , ' Name' )
42+ $getADGroupsSplatParams = @ {
43+ Filter = $filter
44+ Properties = $properties
45+ Server = $pdc
46+ ErrorAction = ' Stop'
47+ }
48+ if ([String ]::IsNullOrEmpty($searchOUs )) {
49+ Write-Information " Querying AD groups that match filter [$ ( $filter ) ]"
50+ $groups = Get-ADGroup @getADGroupsSplatParams | Select-Object $properties
51+ }
52+ else {
53+ $groups = foreach ($searchOU in $searchOUs ) {
54+ Write-Information " Querying AD groups that match filter [$ ( $filter ) ] in OU [$ ( $searchOU ) ]"
55+ Get-ADGroup @getADGroupsSplatParams - SearchBase $searchOU | Select-Object $properties
56+ }
57+ }
58+ Write-Information " Successfully queried [$ ( $groups.count ) ] existing groups"
59+
60+ $actionMessage = " returning data to HelloID"
61+ foreach ($group in $groups ) {
62+ $groupMembers = @ ()
63+ $getADGroupMembersSplatParams = @ {
64+ Identity = $group.ObjectGUID
65+ Recursive = $true
66+ Server = $pdc
67+ ErrorAction = ' Stop'
68+ }
69+ $members = Get-ADGroupMember @getADGroupMembersSplatParams
70+ $groupMembers += $members.SID.Value
71+ $numberOfAccounts = $ (($groupMembers | Measure-Object ).Count)
72+
73+ if (-not ([string ]::IsNullOrEmpty($group.Name ))) {
74+ $displayname = $ ($group.Name ).substring(0 , [System.Math ]::Min(100 , $ ($group.Name ).Length))
75+ }
76+ else {
77+ $displayname = $group.ObjectGUID
78+ }
79+
80+ $permission = @ {
81+ PermissionReference = @ {
82+ Reference = $permissionReference
83+ }
84+ DisplayName = " Permission - $permissionDisplayName "
85+ SubPermissionReference = @ {
86+ Id = $group.ObjectGUID
87+ }
88+ SubPermissionDisplayName = $displayName
89+ }
90+
91+ # Batch permissions based on the amount of account references,
92+ # to make sure the output objects are not above the limit
93+ $accountsBatchSize = 500
94+ if ($numberOfAccounts -gt 0 ) {
95+ $accountsBatchSize = 500
96+ $batches = 0 .. ($numberOfAccounts - 1 ) | Group-Object { [math ]::Floor($_ / $accountsBatchSize ) }
97+ foreach ($batch in $batches ) {
98+ $permission.AccountReferences = [array ]($batch.Group | ForEach-Object { @ ($groupMembers [$_ ]) })
99+ Write-Output $permission
100+ }
101+ }
102+ }
103+ Write-Information ' Target sub-permissions import completed'
104+ }
105+ catch {
106+ $ex = $PSItem
107+ $auditMessage = " Error $ ( $actionMessage ) . Error: $ ( $ex.Exception.Message ) "
108+ $warningMessage = " Error at Line [$ ( $ex.InvocationInfo.ScriptLineNumber ) ]: $ ( $ex.InvocationInfo.Line ) . Error: $ ( $ex.Exception.Message ) "
109+ Write-Warning $warningMessage
110+ Write-Error $auditMessage
111+ }
0 commit comments