Creating, updating and maintaining Active Directory group members can be a boring time intensive job. The following script creates new groups for all kind of unique job titles available in Active Directory. After the creation of all groups, it loops through all users and add these to the created groups in the function above. These scripts help you to maintain for example role base access.
* This script is made a long time ago, comment if things are not working on newer AD versions.
This script contains 3 functions:
1. Creating groups for all the unique job title’s available.
2. Adding users with the same Job title
3. Remove users after job title change
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 |
<# .SYNOPSIS Dynamic ADGroup population with PowerShell. .DESCRIPTION This scripts contains 3 functions: 1. Creating groups for all the unique jobtitle's available. 2. Adding users with the same Jobtitle 3. Remove users after jobtitle change .PARAMETER <Parameter_Name> $DynamicGroupOU = "OU=Functions,OU=Company,OU=Contoso,DC=contoso,DC=com" $GroupScope = "global" $logfile = "C:\Temp\Joblog.log" .INPUTS CreateNewJobTitleGroups -usersWithJobTitle $usersWithJobTitle -DynamicGroupOU $DynamicGroupOU -GroupScope $GroupScope AddUsersJobTitleGroups -DynamicGroupOU $DynamicGroupOU RemoveUsersJobTitleGroups -DynamicGroupOU $DynamicGroupOU .OUTPUTS Log file stored in C:\Windows\Temp\<name>.log> .NOTES Version: 1.0 Author: Rob Verhees Creation Date: 16-11-2018 Purpose/Change: Initial script development #> Function Write-Log { [CmdletBinding()] Param( [Parameter(Mandatory=$False)] [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")] [String] $Level = "INFO", [Parameter(Mandatory=$True)] [string] $Message, [Parameter(Mandatory=$False)] [string] $logfile ) $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") $Line = "$Stamp $Level $Message" If($logfile) { Add-Content $logfile -Value $Line } Else { Write-Output $Line } } Function CreateNewJobTitleGroups { Param( $usersWithJobTitle, $DynamicGroupOU, $GroupScope ) $jobGroups = $usersWithJobTitle | Group-Object title $UniqueJobGroups = ($jobGroups | select group).group.title | select -Unique $_ Foreach($Unique in $UniqueJobGroups) { $adGroup = Get-ADGroup -SearchBase $DynamicGroupOU -Filter * | where{$_.name -like $Unique} if(!$adGroup){ try { write-log -Level INFO -Message "Creating new group: $unique" -logfile $logfile New-ADGroup -GroupScope $GroupScope -Path $DynamicGroupOU -Name $Unique } Catch { write-log -Level ERROR -Message "Failed to create group: $unique" -logfile $logfile Write-log -Level ERROR -Message $_.exception.message -logfile $logfile } } } } function AddUsersJobTitleGroups { Param ( $DynamicGroupOU ) $adGroups = Get-ADGroup -SearchBase $DynamicGroupOU -Filter * foreach($adGroup in $adGroups){ $adRoleMembers = get-aduser -Filter * -Properties sAMAccountName,Title | where {$_.title -eq $adGroup.name -and $_.enabled -eq $true} foreach($adRoleMember in $adRoleMembers){ if(($adgroup | Get-ADGroupMember | where{$_.ObjectGUID -eq $($adRoleMember.ObjectGUID)} | select -ExpandProperty ObjectGUID) -ne $adRoleMember.ObjectGUID){ write-log -Level INFO -Message "Adding new member: $($adrolemember.name) to $($adgroup.Name)" -logfile $logfile Try { Add-ADGroupMember $adGroup -Members $adRoleMember } Catch { Write-Log -Level ERROR -Message $_.exception.message -logfile $logfile } } } } } function RemoveUsersJobTitleGroups { Param ( $DynamicGroupOU ) $adGroups = Get-ADGroup -SearchBase $DynamicGroupOU -Filter * foreach($adGroup in $adGroups){ $currentAdGroupMembers = Get-ADGroupMember $adGroup foreach($currentAdGroupMember in $currentAdGroupMembers) { if((Get-ADUser -Identity $currentAdGroupMember.objectGUID.guid -Properties title | select -ExpandProperty title) -ne $adGroup.name){ write-log -Level INFO -Message "Removing: $($currentAdgroupmember.name) because JobTitle is changed" -logfile $logfile Try { Remove-ADGroupMember -identity $adGroup -members $currentAdGroupMember -Confirm:$false } Catch { Write-log -Level ERROR -Message $_.exception.message -logfile $logfile } } Elseif((Get-ADUser -Identity $currentAdGroupMember.objectGUID.guid | select -ExpandProperty Enabled) -eq $false ){ write-log -Level INFO -Message "Removing Disabled User: $($currentAdGroupMember.name)" -logfile $logfile Try { Remove-ADGroupMember -identity $adGroup -members $currentAdGroupMember -Confirm:$false } Catch { Write-log -Level ERROR -Message $_.exception.message -logfile $logfile } } Else { #Do Nothing } } } } $usersWithJobTitle = get-aduser -Filter * -Properties sAMAccountName,Title | where {$_.title -notlike $null -and $_.enabled -eq $true} $DynamicGroupOU = "OU=Functions,OU=Company,OU=Contoso,DC=contoso,DC=com" $GroupScope = "global" $logfile = "C:\Temp\Joblog.log" CreateNewJobTitleGroups -usersWithJobTitle $usersWithJobTitle -DynamicGroupOU $DynamicGroupOU -GroupScope $GroupScope AddUsersJobTitleGroups -DynamicGroupOU $DynamicGroupOU RemoveUsersJobTitleGroups -DynamicGroupOU $DynamicGroupOU |