#requires -version 7 | |
using namespace Microsoft.Graph.PowerShell.Models | |
using namespace System.Collections.Generic | |
function Get-MgServicePrincipalPermission { | |
param( | |
[Parameter(ParameterSetName='Id',ValueFromPipelineByPropertyName)][Alias('Id')][string]$ServicePrincipalId, | |
[Parameter(ParameterSetName='Object',ValueFromPipeline)][Microsoft.Graph.PowerShell.Models.MicrosoftGraphServicePrincipal]$ServicePrincipal | |
) | |
begin { | |
#We use this to cache app info for permission lookups | |
[Dictionary[string,MicrosoftGraphServicePrincipal]]$spCache = @{} | |
} | |
process { | |
$ErrorActionPreference = 'Stop' | |
$ServicePrincipal ??= Get-MgServicePrincipal -ServicePrincipalId $ServicePrincipalId | |
$ServicePrincipalId ??= $ServicePrincipal.Id | |
$appPermissions = Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $ServicePrincipalId | |
| Where-Object DeletedDateTime -eq $null | |
$delegatedPermissions = Get-MgServicePrincipalOauth2PermissionGrant -ServicePrincipalId $ServicePrincipalId | |
foreach ($app in $appPermissions) { | |
$spCache[$app.ResourceId] ??= Get-MgServicePrincipal -ServicePrincipalId $app.ResourceId | |
[MicrosoftGraphAppRole]$role = $spCache[$app.ResourceId].AppRoles | |
| Where-Object Id -eq $app.AppRoleId | |
if (-not $Role) {throw "No matching permission found for AppRoleID $($app.AppRoleId). This is a bug"} | |
[PSCustomObject]@{ | |
ServicePrincipalName = $ServicePrincipal.DisplayName | |
Id = $app.Id | |
Type = 'Application' | |
User = $null | |
ResourceName = $app.ResourceDisplayName | |
Permission = $role.Value | |
PermissionDisplayName = $role.DisplayName | |
Description = $role.Description | |
CreatedDateTime = $app.CreatedDateTime | |
} | |
} | |
foreach ($permission in $delegatedPermissions) { | |
$spCache[$permission.ResourceId] ??= Get-MgServicePrincipal -ServicePrincipalId $permission.ResourceId | |
$resource = $spCache[$permission.ResourceId] | |
foreach ($scope in $permission.Scope.split(' ')) { | |
$role = $resource.AppRoles | Where-Object Value -eq $scope | |
[PSCustomObject]@{ | |
ServicePrincipalName = $ServicePrincipal.DisplayName | |
Id = $app.Id | |
Type = 'Delegated' | |
User = $permission.ConsentType -eq 'AllPrincipals' ? 'All' : $permission.PrincipalId | |
ResourceName = $resource.DisplayName | |
Permission = $scope | |
PermissionDisplayName = $role.DisplayName | |
Description = $role.Description | |
CreatedDateTime = $null | |
} | |
} | |
} | |
} | |
} |
JustinGrote
commented
Dec 1, 2022
InvalidOperation:
Line |
4 | … ='Object',ValueFromPipeline)][MicrosoftGraphServicePrincipal]$Service …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Unable to find type [MicrosoftGraphServicePrincipal].
InvalidOperation: Line | 4 | … ='Object',ValueFromPipeline)][MicrosoftGraphServicePrincipal]$Service … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Unable to find type [MicrosoftGraphServicePrincipal].
You probably need to import the graph module first before running so that the type is loaded.
InvalidOperation: Line | 4 | … ='Object',ValueFromPipeline)][MicrosoftGraphServicePrincipal]$Service … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Unable to find type [MicrosoftGraphServicePrincipal].
You probably need to import the graph module first before running so that the type is loaded.
I've updated Microsoft.Graph module and imported it, but the same error is displayed after run your Powershell script.
Try running using namespace Microsoft.Graph.PowerShell.Models
before running the function. I also updated the function so it shouldn't error if this is missing now.
Try running
using namespace Microsoft.Graph.PowerShell.Models
before running the function. I also updated the function so it shouldn't error if this is missing now.
InvalidOperation:
Line |
8 | [Dictionary[string,MicrosoftGraphServicePrincipal]]$spCache = @{}
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Unable to find type [Dictionary].
Get-MgServicePrincipalPermission: Cannot index into a null array.
Are you running this in PowerShell 5.1? Try PowerShell 7, also try dot sourcing the script rather than cut-paste. For whatever reason you are not getting the namespace things at the top of the script to register correctly.
Are you running this in PowerShell 5.1? Try PowerShell 7, also try dot sourcing the script rather than cut-paste. For whatever reason you are not getting the namespace things at the top of the script to register correctly.
I'm running PowerShell 7.3.5. I'm trying to run Powershell script in another VM.
@rdantas9 works fine for me, not sure how you're invoking it that is causing the namespace errors.
@rdantas9 works fine for me, not sure how you're invoking it that is causing the namespace errors.
I was able to run your PowerShell script properly, but I've replaced line 12 from MicrosoftGraphServicePrincipal to Microsoft.Graph.PowerShell.Models.MicrosoftGraphServicePrincipal.
Thanks for your patience and support!