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 2 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
35 changes: 23 additions & 12 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,19 @@ function PageAll-GraphRequest($initialUrl, $logFilePath) {

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

function Check-Team($group) {
function Check-Team($IncludeNonSDSGroups, $group) {
acherrymsft marked this conversation as resolved.
Show resolved Hide resolved
if (($group.resourceProvisioningOptions -ne $null) -and $group.resourceProvisioningOptions.Contains("Team") -and $group.mailNickname.StartsWith("Section_")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to also remove the last filter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

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=isMembershipLimitedToOwners,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 && $result.isMembershipLimitedToOwners -eq $false){
AyronJohnson marked this conversation as resolved.
Show resolved Hide resolved
return $result
}
return $false
}
return ($result -ne $null -and (-Not $result.ContainsKey("error")))
}
catch {
Expand All @@ -141,21 +151,22 @@ 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')" + $(if(-Not $IncludeNonSDSGroups) {"+and+startswith(mailNickname,'Section_')"}) + "+and+resourceProvisioningOptions/Any(x:x+eq+'Team')&$groupSelectClause"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be made easier to read. Either:
$initialSDSGroupUri = "https://graph.microsoft.com/beta/groups?$groupSelectClause&`$filter=groupTypes/any(c:c+eq+'Unified')+and+resourceProvisioningOptions/Any(x:x+eq+'Team')" + $(if(-Not $IncludeNonSDSGroups) {"+and+startswith(mailNickname,'Section_')"})

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pertinent part of this was to eliminate the inline, middle of the string if (-Not $includeNonSdsGroups) and make it a possible suffix, to limit the risk of the $select statement being ignored. Your new version still has the $filter at the start.


$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 +177,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 +186,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 +201,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 +224,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