Aug 10 2022 01:04 PM
Hello,
I need to create a powershell script to add specific Users by Titles to a group in AD and remove anyone created after 93 days.
The first part of my script is as follows:
Import-Module ActiveDirectory
$Days = 92
$Time = (Get-Date).Adddays(-($Days))
$List = "(title -like 'Title1') -or (title -like 'Title2') -or (Description -like 'Description1') -or (Description -like 'Description2')"
Get-ADUser -Filter $List -Properties whenCreated,Title,DisplayName | Where {$_.whenCreated -gt $Time} | Select DisplayName, Title, whenCreated | export-csv c:\temp\OnBoard.csv -NoTypeInformation
As they age, they need to be removed from the group. This is for onboarding new users for Orientation.
How would I write the Add-ADGroupMember in this script with this criteria so that it removes them after 93 days? Leaving just new users created in the last 92 days.
I could not find anything about whenCreated with specific titles, so I did my best to list them and confirmed it is accurate.
Thanks,
Denise
Aug 10 2022 11:00 PM
Solution
Hi, Denise.
Here's a template to get you started.
Points of note:
$HasChanged = $false;
$Threshold = [datetime]::Now.Date.AddDays(-92);
$Filter = { (whenCreated -ge $Threshold) -and ((title -eq "title1") -or (title -eq "title2") -or (description -eq "description1") -or (description -eq "description2")) };
# Retrieve currently-eligible members.
$Memberships = [System.Collections.Generic.List[string]] (Get-ADUser -Filter $Filter).distinguishedName;
# Retrieve the group along with the current membership.
$Group = Get-ADGroup -Identity "<guid> or <dn> or <name>" -Properties members;
#region Expunge ineligible existing members.
# First, expunge anyone from the current membership who does not feature in the current memberhip list.
if ($Group.PropertyNames.Contains("members") -and ($Group.members.Count -gt 0))
{
# We need to copy the existing members out to an array before the "members" collection on the group object.
$Group.Members.CopyTo(($MemberDNs = [string[]]::new($Group.Members.Count)), 0)
foreach ($ExistingMember in $MemberDNs)
{
if (-not $Memberships.Contains($ExistingMember))
{
# Remove the person from the group.
$null = $Group.members.Remove($ExistingMember);
$HasChanged = $true;
}
else
{
# Remove the person from the list of people to add, since they already are a member of the group.
$null = $Memberships.Remove($ExistingMember);
}
}
}
#endregion
#region Add new eligible members
if ($Memberships.Count -gt 0)
{
$HasChanged = $true;
foreach ($NewMember in $Memberships)
{
$null = $Group.members.Add($NewMember);
}
}
#endregion
# Finally, if there's been any changes to the group's membership, commit the changes back to Active Directory.
if ($HasChanged)
{
Set-ADGroup -Instance $Group;
}
# Bada-bing, bada-bang. We're done.
Cheers,
Lain
Aug 11 2022 06:44 AM