# Grant application admin consent via script

**URL:** <https://forums.powershell.org/t/grant-application-admin-consent-via-script/22625>\
**Category:** PowerShell Help\
**Created:** [July 19, 2023, 12:02am UTC](https://forums.powershell.org/t/grant-application-admin-consent-via-script/22625 "2023-07-19T00:02:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CLiFoS](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@CLiFoS](https://forums.powershell.org/u/CLiFoS)\
**Post date:** [July 19, 2023, 12:02am UTC](https://forums.powershell.org/t/grant-application-admin-consent-via-script/22625/1 "2023-07-19T00:02:17Z")

</div>

I’m trying to create a script to ask for a tenancy admin credentials once and:

- create app
- set my account as owner
- create app secret
- add specific application (role, not delegate) API permissions like “User.Read.All” and “Device.Read.All”
- consent admin permissions automatically from the script

I managed to do most of these points using AzureAD module functions but I’m struggling with consent.  
I’d like to avoid having to connect to several services and ideally I don’t want to have to type in my credentials multiple times.

Can someone help with this please?

Appreciate in advance.

C

some code:

```auto

#this is huge so I won't post all of it here
$allPermissions = @(
    @{ PermissionName = "AccessReview.Read.All"; PermissionType = "Role"; PermissionId = "d07a8cc0-3d51-4b77-b3b0-32704d1f69fa" },
    @{ PermissionName = "AccessReview.Read.All"; PermissionType = "Scope"; PermissionId = "ebfcd32b-babb-40f4-a14b-42706e83bd28" },
    @{ PermissionName = "AccessReview.ReadWrite.All"; PermissionType = "Role"; PermissionId = "ef5f7d5c-338f-44b0-86c3-351f46c8bb5f" },
...
)

function Get-GraphPermissions
{
    param (
        [Parameter(Mandatory = $true, ParameterSetName = 'ByName')]
        [string]$PermissionName,

        [Parameter(Mandatory = $true, ParameterSetName = 'ById')]
        [string]$PermissionId,

        [Parameter(Mandatory = $false, ParameterSetName = 'ByName')]
        [Parameter(Mandatory = $false, ParameterSetName = 'ById')]
        [switch]$Role,

        [Parameter(Mandatory = $false, ParameterSetName = 'ByName')]
        [Parameter(Mandatory = $false, ParameterSetName = 'ById')]
        [switch]$Scope
    )

    if ($PSCmdlet.ParameterSetName -eq 'ByName')
    {
        $tmp = $allPermissions | Where-Object { $_.PermissionName -eq $PermissionName }
    }
    if ($PSCmdlet.ParameterSetName -eq 'ById')
    {
        $tmp = $allPermissions | Where-Object { $_.PermissionId -eq $PermissionId }
    }

    if ( $Role.IsPresent )
    {
        return $tmp | Where-Object { $_.PermissionType -eq "Role" }
    }
    elseif ( $Scope.IsPresent )
    {
        return ($tmp | Where-Object { $_.PermissionType -eq "Scope" })
    }
    else
    {
        return $tmp
    }

}

# Connect to Azure AD
$user = Connect-AzureAD

# Create app registration
$appName = "My Test App"

# create app with specific permissions
$myApp = New-AzureADApplication -DisplayName $appName

# create service principal
$newSP = New-AzureADServicePrincipal -AppId $myApp.AppId

# set owner
$admin = Get-AzureADUser -Filter "UserPrincipalName eq '$($user.Account.Id)'"
Add-AzureADApplicationOwner -ObjectId $myApp.ObjectId -RefObjectId $admin.ObjectId

$reqPermissions = @(
    "Device.Read.All",
    "User.Read.All"
)
$allPermissionIds = $reqPermissions | ForEach-Object { (Get-GraphPermissions -PermissionName $_ -Role).PermissionId }

# add required permissions
$req = New-Object -TypeName "Microsoft.Open.AzureAD.Model.RequiredResourceAccess"
$req.ResourceAppId = "00000003-0000-0000-c000-000000000000"
$req.ResourceAccess += $reqPermissions | ForEach-Object { New-Object -TypeName "Microsoft.Open.AzureAD.Model.ResourceAccess" -ArgumentList ((Get-GraphPermissions -PermissionName $_ -Role).PermissionId,"Role") }
Set-AzureADApplication -ObjectId $myApp.ObjectId -RequiredResourceAccess $req

# grant admin consent to the app for these API permissions?

```

---

<div class="post-metadata">

**Author:** ![CLiFoS](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@CLiFoS](https://forums.powershell.org/u/CLiFoS)\
**Post date:** [July 20, 2023, 8:24am UTC](https://forums.powershell.org/t/grant-application-admin-consent-via-script/22625/2 "2023-07-20T08:24:07Z")

</div>

Sorry to be a pain. I’m not asking for the entire working script, some guidance would be very much appreciated.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/grant-application-admin-consent-via-script/22625/3 "2024-05-16T20:14:48Z")

</div>

Closed on May 16, 2024, 8:14pm UTC


