diff --git a/Teams Scripts/Add-GroupOwners-To-Teams.ps1 b/Teams Scripts/Add-GroupOwners-To-Teams.ps1 index 3f258851..e62f006d 100644 --- a/Teams Scripts/Add-GroupOwners-To-Teams.ps1 +++ b/Teams Scripts/Add-GroupOwners-To-Teams.ps1 @@ -17,10 +17,13 @@ .\Add-Group-Owners-To-Teams.ps1 -EducatorUPN john.smith@school.edu -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 @@ -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 { @@ -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 } @@ -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 @@ -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 @@ -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." @@ -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."