|
############################################### |
|
# Author: Miroslav Kačena |
|
# Last Modification: 2025-06-25 |
|
############################################### |
|
|
|
<# |
|
.SYNOPSIS |
|
Creates an app registration in Microsoft Entra ID with Write permissions to the specified SharePoint site. |
|
|
|
.DESCRIPTION |
|
This script registers a new app in Microsoft Entra ID, |
|
creates a new self-signed certificate to use with the application registration, |
|
and sets the Write permissions to the specified SharePoint site. |
|
|
|
.PARAMETER Tenant |
|
The name of the tenant in the format "contoso.onmicrosoft.com". |
|
|
|
.PARAMETER Site |
|
Absolute URL of the SharePoint site. |
|
|
|
.PARAMETER ClientId |
|
The client ID of the app registration used for running PnP PowerShell commands. |
|
|
|
.PARAMETER ApplicationName |
|
The name of the app registration. |
|
|
|
.EXAMPLE |
|
.\Register-EntraIDAppForSharePointSite.ps1 -Tenant "contoso.onmicrosoft.com" -Site "https://contoso.sharepoint.com/sites/mysite" -ClientId "00000000-0000-0000-0000-000000000000" -ApplicationName "MyApp" |
|
|
|
.NOTES |
|
To use this script, an app registration with AllSites.FullControl permission is required for PnP.PowerShell module. If you do not have any, you can register it with this command: |
|
- Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP PowerShell" -Tenant contoso.onmicrosoft.com |
|
- More info: https://pnp.github.io/powershell/articles/registerapplication.html |
|
#> |
|
|
|
[CmdletBinding()] |
|
param ( |
|
[Parameter(Mandatory = $true, HelpMessage = "The name of the tenant in the format 'contoso.onmicrosoft.com'.")] |
|
[string]$Tenant, |
|
|
|
[Parameter(Mandatory = $true, HelpMessage = "Absolute URL of the SharePoint site.")] |
|
[string]$Site, |
|
|
|
[Parameter(Mandatory = $true, HelpMessage = "The client ID of the app registration used for running PnP PowerShell commands.")] |
|
[string]$ClientId, |
|
|
|
[Parameter(Mandatory = $true, HelpMessage = "The name of the app registration.")] |
|
[string]$ApplicationName |
|
) |
|
|
|
$ErrorActionPreference = 'Stop' |
|
|
|
[SecureString]$CertificatePassword = Read-Host -Prompt "Enter certificate password" -AsSecureString |
|
|
|
Write-Host "Registering new app in Entra ID..." |
|
$app = Register-PnPAzureADApp -ApplicationName $ApplicationName -Tenant $Tenant -CertificatePassword $CertificatePassword -SharePointApplicationPermissions "Sites.Selected" -GraphApplicationPermissions "Sites.Selected" |
|
Write-Host "App registered successfully." |
|
|
|
Write-Host "Connecting to the SharePoint site: $Site" |
|
Connect-PnPOnline -Url $Site -Interactive -ClientId $ClientId |
|
Write-Host "Successfully connected to the SharePoint site." |
|
|
|
Write-Host "Granting permissions to the SharePoint site..." |
|
Grant-PnPAzureADAppSitePermission -AppId $app.'AzureAppId/ClientId' -DisplayName $ApplicationName -Site $Site -Permissions Write |
|
Write-Host "Permissions granted successfully." |
|
|
|
Write-Host "Disconnecting from the SharePoint site..." |
|
Disconnect-PnPOnline |
|
Write-Host "Disconnected." |
I don't think you can do all of that with
Register-PnPAzureADAppcommand. From the things you mentioned, I found that only validity length can be adjusted using the-ValidYearsparameter. If you want different start/end date or intended purpose, you need to use other method to create the certificate and then use the existing certificate inRegister-PnPAzureADAppcommand by providing the-CertificatePathparameter.Please see the documentation to the
Register-PnPAzureADAppcommand: https://pnp.github.io/powershell/cmdlets/Register-PnPAzureADApp.html