Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new switch to allow inclusion of all class teams #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions Teams Scripts/Add-GroupOwners-To-Teams.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
.\Add-Group-Owners-To-Teams.ps1 -EducatorUPN [email protected]
-For all SDS Teams
.\Add-Group-Owners-To-Teams.ps1
-For all Teams, including non-SDS provisioned teams
.\Add-Group-Owners-To-Teams.ps1 -IncludeNonSDSGroups
#>

Param (
[Parameter(Mandatory = $false)][string]$EducatorUPN)
[Parameter(Mandatory = $false)][string]$EducatorUPN,
[Parameter(Mandatory = $false)][Switch]$IncludeNonSDSGroups)

function Initialize() {
import-module Microsoft.Graph.Authentication -MinimumVersion 0.9.1
Expand Down Expand Up @@ -127,12 +130,20 @@ function PageAll-GraphRequest($initialUrl, $logFilePath) {

$groupSelectClause = "`$select=id,mailNickname,emailAddress,displayName,resourceProvisioningOptions"

function Check-Team($group) {
if (($group.resourceProvisioningOptions -ne $null) -and $group.resourceProvisioningOptions.Contains("Team") -and $group.mailNickname.StartsWith("Section_")) {
function Check-Team($includeNonSDSGroups, $group) {
if (($group.resourceProvisioningOptions -ne $null) -and $group.resourceProvisioningOptions.Contains("Team") -and ($group.mailNickname.StartsWith("Section_") -or $includeNonSDSGroups)) {
try {
Refresh-Token
$groupId = $group.id
$result = invoke-graphrequest -Method GET -Uri "https://graph.microsoft.com/beta/teams/$groupId/?`$select=id" -ContentType "application/json" -SkipHttpErrorCheck
$result = invoke-graphrequest -Method GET -Uri "https://graph.microsoft.com/beta/teams/$groupId/?`$select=id,classSettings" -ContentType "application/json" -SkipHttpErrorCheck
if($includeNonSDSGroups){
# This will only include class teams in the filtered batch, when including nonSDS groups
if($result.classSettings){
return $result
}
return $false
}
Write-Host
return ($result -ne $null -and (-Not $result.ContainsKey("error")))
}
catch {
Expand All @@ -141,21 +152,23 @@ function Check-Team($group) {
}
}

function Get-SDSTeams($logFilePath) {
$initialSDSGroupUri = "https://graph.microsoft.com/beta/groups?`$filter=groupTypes/any(c:c+eq+'Unified')+and+startswith(mailNickname,'Section_')+and+resourceProvisioningOptions/Any(x:x+eq+'Team')&$groupSelectClause"
function Get-SDSTeams($includeNonSDSGroups, $logFilePath) {
$initialSDSGroupUri = "https://graph.microsoft.com/beta/groups?`$filter=groupTypes/any(c:c+eq+'Unified')+and+resourceProvisioningOptions/Any(x:x+eq+'Team')" + $(if(-Not $includeNonSDSGroups) {"+and+startswith(mailNickname,'Section_')"}) + "&$groupSelectClause"

$unfilteredSDSGroups = PageAll-GraphRequest $initialSDSGroupUri $logFilePath

write-output "Retrieve $($unfilteredSDSGroups.Count) groups." | out-file $logFilePath -Append
$i = 0
$filteredSDSTeams = $unfilteredSDSGroups | Where-Object { (Write-Progress "Validating groups..." -Status "Progress" -PercentComplete (($i++ / $unfilteredSDSGroups.count) * 100)) -or (Check-Team $_) }
$filteredSDSTeams = $unfilteredSDSGroups | Where-Object { (Write-Progress "Validating groups..." -Status "Progress" -PercentComplete (($i++ / $unfilteredSDSGroups.count) * 100)) -or (Check-Team $includeNonSDSGroups $_) }
write-output "Filtered to $($filteredSDSTeams.Count) groups." | out-file $logFilePath -Append
return $filteredSDSTeams
}

function Get-SDSTeams-ForUser($EducatorUPN, $logFilePath) {
function Get-SDSTeams-ForUser($EducatorUPN, $includeNonSDSGroups, $logFilePath) {
$initialOwnedObjectsUri = "https://graph.microsoft.com/beta/users/$EducatorUPN/ownedObjects?$groupSelectClause"
$unfilteredOwnedGroups = PageAll-GraphRequest $initialOwnedObjectsUri $logFilePath
$i = 0
$filteredOwnedGroups = $unfilteredOwnedGroups | Where-Object { (Write-Progress "Validating groups..." -Status "Progress" -PercentComplete (($i++ / $unfilteredOwnedGroups.count) * 100)) -or (Check-Team $_) }
$filteredOwnedGroups = $unfilteredOwnedGroups | Where-Object { (Write-Progress "Validating groups..." -Status "Progress" -PercentComplete (($i++ / $unfilteredOwnedGroups.count) * 100)) -or (Check-Team $includeNonSDSGroups $_) }
return $filteredOwnedGroups
}

Expand All @@ -166,7 +179,7 @@ function Get-Owners-ForGroup($groupId) {
return $filteredOwners
}

function Execute($EducatorUPN, $recordedGroups, $logFilePath) {
function Execute($EducatorUPN, $includeNonSDSGroups, $recordedGroups, $logFilePath) {
$processedTeams = $null

Initialize
Expand All @@ -175,7 +188,7 @@ function Execute($EducatorUPN, $recordedGroups, $logFilePath) {
Write-Host "Obtaining list of SDS Created Teams. Please wait..."
Write-Output "Obtaining list of SDS Created Teams. Please wait..." | out-file $logFilePath -append

$SDSTeams = Get-SDSTeams $logFilePath
$SDSTeams = Get-SDSTeams $includeNonSDSGroups $logFilePath

Write-Host "Identified $($SDSTeams.count) teams that are provisioned."
Write-Output "Identified $($SDSTeams.count) teams that are provisioned." | Out-File $logFilePath -Append
Expand All @@ -190,7 +203,7 @@ function Execute($EducatorUPN, $recordedGroups, $logFilePath) {
else {
Write-Output "Obtaining list of SDS Teams for user $($EducatorUPN), Please wait..." | Out-File $logFilePath -Append
Write-Host "Obtaining list of SDS Teams for user $($EducatorUPN), Please wait..."
$SDSTeams = Get-SDSTeams-ForUser $EducatorUPN $logFilePath
$SDSTeams = Get-SDSTeams-ForUser $EducatorUPN $includeNonSDSGroups $logFilePath

Write-Output "Identified $($SDSTeams.count) teams that are provisioned." | Out-File $logFilePath -Append
Write-Host "Identified $($SDSTeams.count) teams that are provisioned."
Expand All @@ -213,7 +226,7 @@ $logFilePath = ".\Add-Group-Owners-To-Teams.log"
$recordedGroups = ".\Updated-Teams.csv"

try {
Execute $EducatorUPN $recordedGroups $logFilePath
Execute $EducatorUPN $IncludeNonSDSGroups $recordedGroups $logFilePath
}
catch {
Write-Error "Terminal Error occurred in processing."
Expand Down