Hi everyone.
I’ve been learning PowerShell off the side of my desk for awhile now, and I’m learning lots but just scratching the surface. I’ve been working on my first ‘real world’ project and I’ve run into a snag.
Our school just upgraded our ERP system and had to remove a lot of customization to allow us to complete the upgrade. Some of that customization affected a large account export that we use to populate Active Directory with student accounts. The idea is to add the groups the new version is creating into toe groups the old ERP had previously created in order to maintain student access. Unfortunately, some time in the past, the school wanted to denote certain programs, so they added extra characters into the name. So the naming convention isn’t the same for all groups. Here’s a basic synopsis.
Start by getting a list of the groups that have access associated with them. For each group in the list, do the following.
- Add our standard Active Directory prefix for data access groups
- Find the group.
- If the group can't be found, add the special identifier and retry
- Remove existing group members (as the import script no longer touches these groups, we don't want 1st year students seeing 2nd year exams)
- Add the new group as a member of the old group
- Find the new groups that associate with them and add them as members of the original group
Here’s a sample of what I’m trying to do.
Import-Module ActiveDirectory
# Load the OUs we need into a variable. The real script gets them from AD.
$OUs = 'OU1','OU2','OU3'
# Variable will hold groups that don't follow standard naming conventions
$NoGroup = @()
# Process the groups
$OUs | foreach {
# Build the old group name
$OldGroup = 'DtaG_Student_' + $_ + '_1Fa'
# Build the new group name
$NewGroup = 'DtaG_Student_' + $_ + '_190901Fa-'
try
{
# Get-ADGroup $OldGroup
# Get group members
$Members = Get-ADGroupMember $OldGroup
# Make sure last year's students aren't still in the old group
Remove-ADGroupMember $Oldgroup -Members $Members -whatif
# Add the new group to the old group
Add-ADGroupMember $OldGroup -Members $NewGroup -whatif
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
$NoGroup += $OldGroup
Write-Host $OldGroup NOT FOUND
# if the old group was one of the TEP groups, change the Old Group format to DtaG_Student_TEP_$OU_1Fa,
# then remove existing members, add the new group before looping to the next OU
}
}
I hope this is clear enough to give everyone an idea of what I’m trying to accomplish.
Thanks in advance for any help you can offer!
Derek