Skip to content

Commit

Permalink
Merge pull request KelvinTegelaar#484 from rvdwegen/dev
Browse files Browse the repository at this point in the history
Add option to add owners/members to azurerole and generic groups
  • Loading branch information
KelvinTegelaar authored Nov 18, 2023
2 parents 823188a + 3ed3e70 commit d89c069
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions AddGroup/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ $results = foreach ($tenant in $SelectedTenants) {
try {
$email = if ($groupobj.domain) { "$($groupobj.username)@$($groupobj.domain)" } else { "$($groupobj.username)@$($tenant)" }
if ($groupobj.groupType -in "Generic", "azurerole", "dynamic") {

$BodyToship = [pscustomobject] @{
"displayName" = $groupobj.Displayname
"description" = $groupobj.Description
"mailNickname" = $groupobj.username
mailEnabled = [bool]$false
securityEnabled = [bool]$true
isAssignableToRole = [bool]($groupobj | Where-Object -Property groupType -EQ "AzureRole")

}
}
if ($groupobj.membershipRules) {
$BodyToship | Add-Member -NotePropertyName "membershipRule" -NotePropertyValue ($groupobj.membershipRules)
$BodyToship | Add-Member -NotePropertyName "groupTypes" -NotePropertyValue @("DynamicMembership")
$BodyToship | Add-Member -NotePropertyName "membershipRuleProcessingState" -NotePropertyValue "On"
}
if ($groupobj.AddOwner -AND $groupobj.groupType -in "generic", "azurerole", "security") {
$BodyToship | Add-Member -NotePropertyName "[email protected]" -NotePropertyValue (($groupobj.AddOwner) | ForEach-Object { "https://graph.microsoft.com/v1.0/users/$($_.value)" })
}
if ($groupobj.AddMember -AND $groupobj.groupType -in "generic", "azurerole", "security") {
$BodyToship | Add-Member -NotePropertyName "[email protected]" -NotePropertyValue (($groupobj.AddMember) | ForEach-Object { "https://graph.microsoft.com/v1.0/users/$($_.value)" })
}
$GraphRequest = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/groups" -tenantid $tenant -type POST -body (ConvertTo-Json -InputObject $BodyToship -Depth 10) -verbose
}
else {
Expand All @@ -43,6 +48,7 @@ $results = foreach ($tenant in $SelectedTenants) {
RequireSenderAuthenticationEnabled = [bool]!$groupobj.AllowExternal
}
$GraphRequest = New-ExoRequest -tenantid $tenant -cmdlet "New-DistributionGroup" -cmdParams $params
# At some point add logic to use AddOwner/AddMember for New-DistributionGroup, but idk how we're going to brr that - rvdwegen
}
"Successfully created group $($groupobj.displayname) for $($tenant)"
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $tenant -message "Created group $($groupobj.displayname) with id $($GraphRequest.id) " -Sev "Info"
Expand All @@ -51,7 +57,6 @@ $results = foreach ($tenant in $SelectedTenants) {
catch {
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $tenant -message "Group creation API failed. $($_.Exception.Message)" -Sev "Error"
"Failed to create group. $($groupobj.displayname) for $($tenant) $($_.Exception.Message)"

}
}
$body = [pscustomobject]@{"Results" = @($results) }
Expand All @@ -60,4 +65,4 @@ $body = [pscustomobject]@{"Results" = @($results) }
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $Body
})
})

0 comments on commit d89c069

Please sign in to comment.