Hi we are trying to add users to a group based on the values of their "Office" and "Description" attributes within Active Directory.
We have populated the below script to search a defined hashtable for the values however this doesn't seem to be populating the results we are after.
All advice welcome!
Import-Module Adaxes
$officeDescriptionToGroup = @{
"Site1-Job1" = "CN=ROLE_Test1,OU=Site1,OU=Site,OU=Domain Role Groups,DC=company,DC=com"
"Site2-Job2" = "CN=ROLE_Test2,OU=Site2,OU=Site,OU=Domain Role Groups,DC=company,DC=com"
}
$userOffice = $Context.TargetObject.OfficeLocations
$userDescription = $Context.TargetObject.Description
$combination = "$userOffice-$userDescription"
if ($officeDescriptionToGroup.ContainsKey($combination)) {
$groupDN = $officeDescriptionToGroup[$combination]
$userObject = Get-AdmUser -Filter { Name -eq $Context.TargetObject.Name }
if ($userObject -ne $null) {
Add-AdmGroupMember -DN $groupDN -Members $userObject
$Context.LogMessage("User $($Context.TargetObject.Name) added to group $groupDN.")
} else {
$Context.LogMessage("Error: User $($Context.TargetObject.Name) not found.")
}
}
else {
$Context.LogMessage("No matching criteria found for User $($Context.TargetObject.Name).")
}