Hello @Krupa Gundraju (Larsen & Toubro Infotech Limit)
Thank you for reaching out. Delegated permissions are used by apps that have a signed-in user present and I made certain changes in your script to generate access token using username and password. You can use this and confirm if it fulfils your requirement.
$TokenBody = 'grant_type=password' + `
'&client_id=12345678-2b13-444b-9631-f2ff99c34e77' + `
'&username=harpreet@contoso.com' +`
'&password=testpassword!' +`
'&resource=https://graph.microsoft.com' +`
'&client_secret=12345~cRUe8o1AIyt7zLx7._NGREHcMhRXCvacgb' +`
'&scope=Policy.Read.All,Policy.ReadWrite.MobilityManagement '
$token = (Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/common/oauth2/token -Body $TokenBody).access_token
$method = "PUT"
$uri = "https://graph.microsoft.com/beta/policies/deviceRegistrationPolicy"
$body = '{
"userDeviceQuota": 1,
"multiFactorAuthConfiguration": "0",
"azureADRegistration": {
"appliesTo": "1",
"isAdminConfigurable": false,
"allowedUsers": [],
"allowedGroups": []
}
}'
Connect-MgGraph -AccessToken $token
Select-MgProfile -Name beta
Invoke-MgGraphRequest -Method $method -uri $uri -Body $body
I hope this helps and resolves your concern.
----------
Please "Accept the answer" if the information helped you. This will help us and others in the community as well.