Skip to content

Instantly share code, notes, and snippets.

@OlafD
Last active October 11, 2018 09:36
Show Gist options
  • Save OlafD/dc6337a1910006bd1c38a64ac9aaaab4 to your computer and use it in GitHub Desktop.
Save OlafD/dc6337a1910006bd1c38a64ac9aaaab4 to your computer and use it in GitHub Desktop.
For Office 365 get information about the ownership for Office 365 groups of a user, given by the mail address. Using this script needs the cmdlets for AzureAD (https://docs.microsoft.com/de-de/azure/active-directory/users-groups-roles/groups-settings-v2-cmdlets).
param (
[string]$UserMail
)
function Ensure-AzureADConnection
{
try
{
# $var = Get-AzureADTenantDetail
$var = Get-AzureADUser -Top 1
}
catch [Microsoft.Open.Azure.AD.CommonLibrary.AadNeedAuthenticationException]
{
Write-Host "You are not connected to Azure Active Directory."
$c = Connect-AzureAD -credential $cred
if ($c -ne $null)
{
$account = $c.Account
$tenant = $c.TenantDomain
Write-Host "Connected as $account to $tenant"
Write-Host
}
}
}
Ensure-AzureADConnection
$output = @()
$filter = "startswith(mail, '$UserMail')"
$user = Get-AzureADUser -Filter $filter
if ($user -eq $null)
{
Write-Host -ForegroundColor Red "User with mail address $UserMail not found"
Quit
}
$userObjectId = $user.ObjectId
Try
{
$groups = $user | Get-AzureADUserMembership -ErrorAction SilentlyContinue
$count = $groups.Count
Write-Host "User $UserMail is member in $count Office 365 groups."
Write-Host
}
Catch
{
}
$hasOutput = $false
foreach ($group in $groups)
{
$groupObjectId = $group.ObjectId
$groupOwners = Get-AzureADGroupOwner -ObjectId $groupObjectId
$ownerCount = $groupOwners.Count
$isOwner = ($groupOwners | Where { $_.ObjectId -eq $userObjectId }).Count -gt 0
if ($isOwner -eq $true)
{
$hasOutput = $true
$element = New-Object PSCustomObject
$element | Add-Member -type NoteProperty -name DisplayName -Value $group.DisplayName
$element | Add-Member -type NoteProperty -name ObjectId -Value $group.ObjectId
# $element | Add-Member -type NoteProperty -name Description -Value $group.Description
$element | Add-Member -type NoteProperty -name OwnerCount -Value $ownerCount
$output += $element
}
}
if ($hasOutput -eq $true)
{
Write-Host "User $UserMail is owner in the following groups:"
$output | Format-Table
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment