On Demand Migration - SharePoint Select Sites - Example script - Assign the application to specific site

Example script to assign Quest on Demand - Migration - SharePoint - Selected Sites application to Sharepoint sites.

The script uses Microsoft Graph, Microsoft Graph Command Line Tools application needs Sites.FullControl.All permission during the assignment. Can revoke it after success assignment.

Create a csv file to provide Sharepoint Site urls:

SiteURL
https://m365x73592402.sharepoint.com/sites/Site1
https://m365x73592402.sharepoint.com/sites/Site2
https://m365x73592402.sharepoint.com/sites/Site3

adjust $FilePath variable in the script to point to the csv file.

Note that this script is provided as-is for example purposes only, and you may need to modify it to work for your specific project.

$FilePath = "c:\temp\Sites.csv" # Define the path for the source file contains site urls

# Install required module if not installed yet
if (Get-Module -ListAvailable -Name Microsoft.Graph) {Write-Host "MS Graph installed"}
else{Write-Host "Installing Microsoft Graph"; install-module microsoft.graph -Repository PSGallery}

# Connect to service, and request Sites.FullControl.All permission
$MgParam = @{
            Scopes = "Sites.FullControl.All"
            NoWelcome = $true
            }
Connect-MgGraph @MgParam

# Import Sharepoint sites
$Sitelist = import-csv $FilePath

# Define the POST body
$AppId = '8990c8ce-4afb-48f0-9e30-f1338ef249db' # ApplicationID
$AppName = 'Quest on Demand - Migration - SharePoint - Selected Sites' # Application Name
$Body = @"
{
'roles': ['fullcontrol'],
'grantedToIdentities': [{
'application': {
'id': '$AppId',
'displayName': '$AppName'
 }
 }]
}
"@

# Walk through on each site and set the permission
foreach ($site in $Sitelist){
$SPUrl = ($site | Out-String | % {[Regex]::Matches($_, "(?<=https://)((.|\n)*?)(?=/)")}).value 
$SiteUrl = ($Site.SiteURL -split "https://"+$SPUrl)
$QueryUrl = "https://graph.microsoft.com/v1.0/sites/"+$SPUrl+":"+$SiteUrl -replace " "
$TheSite = Invoke-MGGraphRequest -Method GET -Uri $QueryUrl # Get SiteID
$results = Invoke-MGGraphRequest -Method POST -Uri ('https://graph.microsoft.com/v1.0/sites/'+$thesite.id+'/permissions') -Body $Body # Assign the application to it
write-host ($results.grantedToIdentities.application.id+" "+$results.roles+" "+$site.SiteURL)
} # end of foreach sites

Disconnect-MgGraph