Public/Grant-EntraIDPermissions.ps1

function Grant-EntraIDPermissions {
    <#
    .SYNOPSIS
    Grants permissions to an Entra ID application on a SharePoint Online site.

    .DESCRIPTION
    Grants Read, Write, Manage or FullControl permissions to an Entra ID application.

    .PARAMETER TenantUrl
    URL of the SharePoint Online tenant administration site.

    .PARAMETER ClientId
    Client ID of the Entra ID application used to connect to SharePoint Online.

    .PARAMETER AppId
    Client ID of the application receiving the permissions.

    .PARAMETER DisplayName
    Display name associated with the permission grant.

    .PARAMETER SiteUrl
    URL of the SharePoint Online site collection.

    .PARAMETER Permissions
    Permissions to grant: Read, Write, Manage or FullControl.
    #>


    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidatePattern('^https://[^/]+-admin\.sharepoint\.com/?$')]
        [string]$TenantUrl,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ClientId,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$AppId,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$DisplayName,

        [Parameter(Mandatory)]
        [ValidatePattern('^https://')]
        [string]$SiteUrl,

        [Parameter(Mandatory)]
        [ValidateSet('Read', 'Write', 'Manage', 'FullControl')]
        [string]$Permissions
    )

    Connect-PnPOnline -Url $TenantUrl -Interactive -ClientId $ClientId

    Grant-PnPEntraIDAppSitePermission `
        -AppId $AppId `
        -DisplayName $DisplayName `
        -Site $SiteUrl `
        -Permissions $Permissions
}