Skip to content

Commit 6c40cee

Browse files
authored
Fix resource script multiple groups found (#46)
* Fix: Multiple groups found Also fixed issue #42 * Fix: Alt + Shift + F * Fix: changes after review * Fix: department default used for select unique
1 parent e6e8ebc commit 6c40cee

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

permissions/groups/subPermission.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ try {
108108
Write-Verbose ("Contract in condition: {0}" -f $contract.Context.InConditions)
109109
if ($contract.Context.InConditions -OR ($actionContext.DryRun -eq $true)) {
110110
# Correlation values
111-
$correlationProperty = "DisplayName" # The AD group property that contains the unique identifier (DisplayName | sAMAccountname | Description)
111+
$correlationProperty = "ExtensionAttribute1" # The AD group property that contains the unique identifier (DisplayName | sAMAccountname | Description)
112112
$correlationValue = $contract.Department.ExternalId # The HelloID resource property that contains the unique identifier
113113

114-
$correlationValue = Get-ADSanitizedGroupName -Name $correlationValue
114+
# Use the Get-ADSanitizedGroupName function if data manipulation is needed. For example, when using a name instead of a code or when you also use this function in the resource script
115+
# $correlationValue = Get-ADSanitizedGroupName -Name $correlationValue
115116

116117
# Get group to use objectGuid to support name change and even correlationProperty change
117118
$group = $null

resources/groups/resources.ps1

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ $correlationProperty = "ExtensionAttribute1" # The AD group property that contai
2323
$correlationValue = "ExternalId" # The HelloID resource property that contains the unique identifier
2424

2525
# Additionally set resource properties as required
26-
$requiredFields = @("ExternalId", "Name", "Code") # If title is used
27-
# $requiredFields = @("ExternalId", "DisplayName") # If department is used
26+
$requiredFields = @("ExternalId", "DisplayName") # If department is used
27+
# $requiredFields = @("ExternalId", "Name", "Code") # If title is used
2828

2929
$resourceData = $resourceContext.SourceData
3030
# Example below for when the externalID is a combination of values
@@ -33,7 +33,8 @@ $resourceData = $resourceContext.SourceData
3333
# $_.ExternalId = $_.Code + "_" + $_.DepartmentCode
3434
# }
3535

36-
$resourceData = $resourceData | Select-Object -Unique ExternalId, Name, Code #, DepartmentCode
36+
$resourceData = $resourceData | Select-Object -Unique ExternalId, DisplayName # If department is used
37+
# $resourceData = $resourceData | Select-Object -Unique ExternalId, Name, Code # If title is used
3738

3839
#region Supporting Functions
3940
function Remove-StringLatinCharacters {
@@ -245,8 +246,7 @@ try {
245246
# https://www.ietf.org/rfc/rfc2253.txt
246247

247248
# Best practice to use the id of the resource to avoid max char limitations and issues in case of name change
248-
$samaccountname = ("title_" + "$($resource.ExternalId)")
249-
$groupName = $resource.Name
249+
$groupName = $resource.DisplayName
250250
# # Other example to use name of resource:
251251
# $groupName = ("department_" + "$($resource.ExternalId)")
252252
# $groupName = ("title_" + "$($resource.Name)")
@@ -260,7 +260,7 @@ try {
260260

261261
# Example when correlationValue is extensionAttribute1
262262
$ADGroupParams = @{
263-
SamAccountName = $samaccountname
263+
SamAccountName = $groupName
264264
Name = $groupName
265265
DisplayName = $groupName
266266
OtherAttributes = @{'extensionAttribute1' = "$correlationValueOutput" }
@@ -315,29 +315,35 @@ try {
315315
if ($actionContext.Configuration.isDebug -eq $true) { Write-Information "Debug: Group parameters: $($ADGroupParams | ConvertTo-Json)" }
316316
}
317317
}
318+
elseif (($currentADGroup | Measure-Object).count -gt 1) {
319+
$outputContext.AuditLogs.Add([PSCustomObject]@{
320+
Message = "Multiple groups found where [$($correlationProperty)] = [$($correlationValueOutput)] for resource [$($resource | ConvertTo-Json)]."
321+
Action = "CreateResource"
322+
IsError = $true
323+
})
324+
}
318325
else {
319-
if($actionContext.Configuration.renameResources -and ($currentADGroup.Name -ne $groupName -or $currentADGroup.DisplayName -ne $groupName))
320-
{
326+
if ($actionContext.Configuration.renameResources -and ($currentADGroup.Name -ne $groupName -or $currentADGroup.DisplayName -ne $groupName)) {
321327
if (-Not($actionContext.DryRun -eq $True)) {
322328

323-
Write-Information "Debug: Group where [$($correlationProperty)] = [$($correlationValue)] already exists, but will be renamed"
329+
if ($actionContext.Configuration.isDebug -eq $true) { Write-Information "Debug: Group where [$($correlationProperty)] = [$($correlationValueOutput)] already exists, but will be renamed" }
324330

325331
$SetADGroupParams = @{
326-
Identity = $currentADGroup.objectguid
327-
DisplayName = $groupName
328-
Server = $pdc
332+
Identity = $currentADGroup.objectguid
333+
DisplayName = $groupName
334+
Server = $pdc
329335
}
330336
$null = Set-AdGroup @SetADGroupParams
331337

332338
$RenameADGroupParams = @{
333-
Identity = $currentADGroup.objectguid
334-
NewName = $groupName
335-
Server = $pdc
339+
Identity = $currentADGroup.objectguid
340+
NewName = $groupName
341+
Server = $pdc
336342
}
337343
$null = Rename-ADObject @RenameADGroupParams
338344

339345
$outputContext.AuditLogs.Add([PSCustomObject]@{
340-
Message = "Renaming group [$($correlationProperty)] = [$($correlationValue)] for resource [$($resource | ConvertTo-Json)]."
346+
Message = "Renaming group [$($correlationProperty)] = [$($correlationValueOutput)] for resource [$($resource | ConvertTo-Json)]."
341347
Action = "CreateResource"
342348
IsError = $false
343349
})
@@ -347,8 +353,7 @@ try {
347353
if ($actionContext.Configuration.isDebug -eq $true) { Write-Information "Debug: Group parameters: $($ADGroupParams | ConvertTo-Json)" }
348354
}
349355
}
350-
else
351-
{
356+
else {
352357
# Create new group if group does not exist yet
353358
if (-Not($actionContext.DryRun -eq $True)) {
354359
if ($actionContext.Configuration.isDebug -eq $true) {

0 commit comments

Comments
 (0)