|
[CmdletBinding(DefaultParameterSetName = 'Default')] |
|
param ( |
|
[Parameter(ParameterSetName = 'ByServicePrincipalId')] |
|
[ValidateNotNullOrEmpty()] |
|
[string] |
|
$ServicePrincipalId, |
|
|
|
[Parameter(ParameterSetName = 'ByDisplayName')] |
|
[ValidateNotNullOrEmpty()] |
|
[string] |
|
$DisplayName, |
|
|
|
[Parameter(ParameterSetName = 'Default')] |
|
[switch] |
|
$All |
|
) |
|
|
|
# Get the EXO and SPO service principal ID |
|
$exo_resource_id = (Get-MgServicePrincipal -Filter "DisplayName eq 'Office 365 Exchange Online'").Id |
|
$spo_resource_id = (Get-MgServicePrincipal -Filter "DisplayName eq 'Office 365 SharePoint Online'").Id |
|
|
|
switch ($PSCmdlet.ParameterSetName) { |
|
ByServicePrincipalId { # Get specific service pricipal object by ID |
|
try { |
|
$all_sp = @(Get-MgServicePrincipal -ServicePrincipalId $ServicePrincipalId -ErrorAction Stop) |
|
} |
|
catch { |
|
Write-Error $_.Exception.Message |
|
return $null |
|
} |
|
} |
|
ByDisplayName { # Get specific service pricipal object by displayname |
|
try { |
|
$all_sp = @(Get-MgServicePrincipal -Filter "DisplayName eq '$($DisplayName)'" -ErrorAction Stop) |
|
} |
|
catch { |
|
Write-Error $_.Exception.Message |
|
return $null |
|
} |
|
} |
|
Default { # Get all service principal objects |
|
$all_sp = @(Get-MgServicePrincipal -All) |
|
} |
|
} |
|
|
|
if ($all_sp.Count -lt 1) { |
|
return $null |
|
} |
|
|
|
$total = $all_sp.Count |
|
|
|
for ($i = 0 ; $i -lt $total ; $i++) { |
|
# Calculate the percentage completed |
|
$percentComplete = [math]::Round(($i / $total) * 100, 2) |
|
|
|
# Display the progress bar |
|
Write-Progress -Activity "Processing Service Principals [$($i+1) / $total]" ` |
|
-Status "Processing: $($all_sp[$i].DisplayName)" ` |
|
-PercentComplete $percentComplete |
|
|
|
# Get the delegated permissions |
|
$delegated_permisions = Get-MgServicePrincipalOauth2PermissionGrant -ServicePrincipalId $all_sp[$i].Id |
|
|
|
# Filter permissions for Exchange and SharePoint Online |
|
$exo_permissions = ($delegated_permisions | Where-Object { $_.ResourceId -eq $exo_resource_id } | Select-Object -Unique Scope).Scope -join "," -replace " ", "," |
|
$spo_permissions = ($delegated_permisions | Where-Object { $_.ResourceId -eq $spo_resource_id } | Select-Object -Unique Scope).Scope -join "," -replace " ", "," |
|
|
|
# If either Exchange Online or SharePoint Online permissions are found, create the custom object |
|
if ($exo_permissions -or $spo_permissions) { |
|
[PSCustomObject]@{ |
|
DisplayName = $all_sp[$i].DisplayName |
|
Id = $all_sp[$i].Id |
|
SignInAudience = $all_sp[$i].SignInAudience |
|
ServicePrincipalType = $all_sp[$i].ServicePrincipalType |
|
ExchangeOnlineDelegatedPermissions = $(if ($exo_permissions) { $exo_permissions } else { 'None' }) |
|
SharePointOnlineDelegatedPermissions = $(if ($spo_permissions) { $spo_permissions } else { 'None' }) |
|
} |
|
} |
|
} |