Skip to content

Instantly share code, notes, and snippets.

@vexx32

vexx32/ExceptionHandling.ps1 Secret

Created Sep 6, 2019
Embed
What would you like to do?
# Process the groups
$OUs | ForEach-Object {
# Build the old group name
$OldGroup = "DtaG_Student_${_}_1Fa"
# Build the new group name
$NewGroup = "DtaG_Student_${_}_190901Fa-"
try {
# 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-Error "$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
}
catch {
# Log additional errors, and either rethrow the error or pass it to Write-Error
# depending on whether you want execution to terminate or keep going through the
# rest of your OUs.
# Rethrow (terminate execution) options:
# 1. $PSCmdlet.ThrowTerminatingError($_)
# 2. throw $_
# 3. $_ | Write-Error -ErrorAction Stop
# Otherwise just write-error:
# $_ | Write-Error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment