Skip to content

Commit e6e8ebc

Browse files
feat: added renaming of resources (#45)
1 parent a529934 commit e6e8ebc

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

configuration.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
"required": false
1111
}
1212
},
13+
{
14+
"key": "renameResources",
15+
"type": "checkbox",
16+
"defaultValue": false,
17+
"templateOptions": {
18+
"label": "Rename resources",
19+
"description": "When toggled, resources will be renamed",
20+
"required": false
21+
}
22+
},
1323
{
1424
"key": "isDebug",
1525
"type": "checkbox",

resources/groups/resources.ps1

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ try {
124124
, "GroupCategory"
125125
, "GroupScope"
126126
, "extensionAttribute1"
127+
, "objectguid"
127128
)
128129
if ($correlationProperty -notin $properties) {
129130
[void]$properties.Add($correlationProperty)
@@ -244,7 +245,8 @@ try {
244245
# https://www.ietf.org/rfc/rfc2253.txt
245246

246247
# Best practice to use the id of the resource to avoid max char limitations and issues in case of name change
247-
$groupName = ("title_" + "$($resource.ExternalId)")
248+
$samaccountname = ("title_" + "$($resource.ExternalId)")
249+
$groupName = $resource.Name
248250
# # Other example to use name of resource:
249251
# $groupName = ("department_" + "$($resource.ExternalId)")
250252
# $groupName = ("title_" + "$($resource.Name)")
@@ -258,7 +260,7 @@ try {
258260

259261
# Example when correlationValue is extensionAttribute1
260262
$ADGroupParams = @{
261-
SamAccountName = $groupName
263+
SamAccountName = $samaccountname
262264
Name = $groupName
263265
DisplayName = $groupName
264266
OtherAttributes = @{'extensionAttribute1' = "$correlationValueOutput" }
@@ -314,21 +316,55 @@ try {
314316
}
315317
}
316318
else {
317-
# Create new group if group does not exist yet
318-
if (-Not($actionContext.DryRun -eq $True)) {
319-
if ($actionContext.Configuration.isDebug -eq $true) {
320-
Write-Information "Debug: Group where [$($correlationProperty)] = [$($correlationValue)] already exists"
319+
if($actionContext.Configuration.renameResources -and ($currentADGroup.Name -ne $groupName -or $currentADGroup.DisplayName -ne $groupName))
320+
{
321+
if (-Not($actionContext.DryRun -eq $True)) {
322+
323+
Write-Information "Debug: Group where [$($correlationProperty)] = [$($correlationValue)] already exists, but will be renamed"
324+
325+
$SetADGroupParams = @{
326+
Identity = $currentADGroup.objectguid
327+
DisplayName = $groupName
328+
Server = $pdc
329+
}
330+
$null = Set-AdGroup @SetADGroupParams
331+
332+
$RenameADGroupParams = @{
333+
Identity = $currentADGroup.objectguid
334+
NewName = $groupName
335+
Server = $pdc
336+
}
337+
$null = Rename-ADObject @RenameADGroupParams
321338

322339
$outputContext.AuditLogs.Add([PSCustomObject]@{
323-
Message = "Skipped creating group $($correlationProperty)] = [$($correlationValue)] for resource [$($resource | ConvertTo-Json)]. (Already exists)"
340+
Message = "Renaming group [$($correlationProperty)] = [$($correlationValue)] for resource [$($resource | ConvertTo-Json)]."
324341
Action = "CreateResource"
325342
IsError = $false
326343
})
327344
}
345+
else {
346+
Write-Warning "DryRun: Group [$($correlationProperty)] = [$($correlationValue)] for resource [$($resource | ConvertTo-Json)] already exists but will be renamed"
347+
if ($actionContext.Configuration.isDebug -eq $true) { Write-Information "Debug: Group parameters: $($ADGroupParams | ConvertTo-Json)" }
348+
}
328349
}
329-
else {
330-
Write-Warning "DryRun: Group $($correlationProperty)] = [$($correlationValue)] for resource [$($resource | ConvertTo-Json)] already exists"
331-
if ($actionContext.Configuration.isDebug -eq $true) { Write-Information "Debug: Group parameters: $($ADGroupParams | ConvertTo-Json)" }
350+
else
351+
{
352+
# Create new group if group does not exist yet
353+
if (-Not($actionContext.DryRun -eq $True)) {
354+
if ($actionContext.Configuration.isDebug -eq $true) {
355+
Write-Information "Debug: Group where [$($correlationProperty)] = [$($correlationValue)] already exists"
356+
357+
$outputContext.AuditLogs.Add([PSCustomObject]@{
358+
Message = "Skipped creating group [$($correlationProperty)] = [$($correlationValue)] for resource [$($resource | ConvertTo-Json)]. (Already exists)"
359+
Action = "CreateResource"
360+
IsError = $false
361+
})
362+
}
363+
}
364+
else {
365+
Write-Warning "DryRun: Group $($correlationProperty)] = [$($correlationValue)] for resource [$($resource | ConvertTo-Json)] already exists"
366+
if ($actionContext.Configuration.isDebug -eq $true) { Write-Information "Debug: Group parameters: $($ADGroupParams | ConvertTo-Json)" }
367+
}
332368
}
333369
}
334370
}

0 commit comments

Comments
 (0)