Last active
February 24, 2023 11:34
-
-
Save CraigChamberlain/cf6cae2cecb8343d1b8a361256d12a83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$removeExcess = $false | |
$count = $sets.Count | |
$i = 0 | |
foreach($set in $filteredSets){ | |
$percent = $i * 100 / $count | |
$i += 1 | |
$teamName = $set.SetCode + " " + $year | |
Write-Progress -Activity "Creating Team" -Status "$percent% Complete | working on $teamName)" -PercentComplete $percent | |
$ProgressPreference = "SilentlyContinue" | |
$team = $existingTeams | Where-Object {$_.DisplayName -eq $teamName} | |
$ProgressPreference = "Continue" | |
if($null -eq $team){ | |
New-Team -Template EDU_Class -DisplayName $teamName > $null # This may be a good place to set Owner?? -Owner $set.Teachers | |
Write-Host "Creating Team: $teamName N.B (run script again to populate)" | |
} | |
else{ | |
Write-Host "Team Already Exists: $teamName" | |
if($team -is [array]){ | |
Write-Warning "`$team is an array skip this pass and investigate $([string]::Join(', ' ,$team.DisplayName))" | |
} | |
else{ | |
#Clearing A Team of users | |
$teamMembers = Get-TeamUser -GroupId $team.GroupId | |
[System.Collections.Generic.HashSet[string]]$existingMembers = $teamMembers | Where-Object {$_.Role -eq "member"} | ForEach-Object {$_.User.ToLower()} | |
if ($null -eq $existingMembers) { | |
$existingMembers = [System.Collections.Generic.HashSet[string]]::new() | |
} | |
[System.Collections.Generic.HashSet[string]]$existingOwners = $teamMembers | Where-Object {$_.Role -eq "owner"} | ForEach-Object {$_.User.ToLower()} | |
if ($null -eq $existingOwners) { | |
$existingMembers = [System.Collections.Generic.HashSet[string]]::new() | |
} | |
[System.Collections.Generic.HashSet[string]]$incomingMembers = $set.Pupils.EmailAddress.ToLower() | |
if ($null -eq $incomingMembers) { | |
$existingMembers = [System.Collections.Generic.HashSet[string]]::new() | |
} | |
$toRemove = [System.Collections.Generic.HashSet[string]]::new($existingMembers) | |
$toRemove.ExceptWith($incomingMembers) | |
$toCreate = [System.Collections.Generic.HashSet[string]]::new($incomingMembers) | |
$toCreate.ExceptWith($existingMembers) | |
if($removeExcess -and $toRemove.Count -gt 0){ | |
foreach($user in $toRemove){ | |
if($user -like "System.Collections.Generic.HashSet[string] new()*"){ Write-Error "inspect" -ErrorAction Stop} | |
Write-Host ("`t Clearing: " + $user) | |
Remove-TeamUser -GroupId $team.GroupId -User $user | |
} | |
} | |
foreach($p in $toCreate){ | |
Write-Host ("`t Adding Member: " + $p) | |
Add-TeamUser -GroupId $team.GroupId -User $p -Role "Member" | |
} | |
foreach($t in $set.Teachers){ | |
if($null -eq $t){continue} | |
if(-not $existingOwners.Remove($t.ToLower())){ | |
Write-Host "`t Adding Owner: $t" | |
Add-TeamUser -GroupId $team.GroupId -User $t -Role "Owner" | |
} | |
$existingOwners.Remove("[email protected]") > $null | |
if($existingOwners.Count -gt 0){ | |
Write-Warning ("`t Check Owners of $teamName`: " + [string]::Join(', ' , $existingOwners )) | |
foreach($user in $existingOwners){ | |
Write-Host ("`t Clearing: " + $user) | |
Remove-TeamUser -GroupId $team.GroupId -User $user | |
} | |
} | |
} | |
} | |
} | |
} | |
Write-Progress -Activity "Creating Team" -Status "Complete" -Completed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment