PATCH appRoles to existing Entra Application using PowerShell Invoke-RestMethod

I am creating BICEP resources for an automated DevOps Pipeline which automates the creation of App Registrations within Microsofts EntraID. The BICEP is using a Microsoft.Resources/deploymentScripts@2023-08-01 resource which hold a piece of script which actually invokes a powershell script within Azure Services. The basic implementation comes from Medium Automate App Registrations.

I modified the script in a way that it also adds some App Roles which must be applied on an (in this case already existing) Application (Application resource).

Currently I can create an Application and also update it with, for example, a secret, api permissions etc… The only thing that returns a BadRequest, is the update for App Roles (AppRoles Resource). I used the example given by Microsoft itself for the specific model in the PowerShell example: AppRoles update example. In my powershell I use the Azure request invocation and soo far it only responds with a BadRequest without any specific message only than ‘An unexpected error occurred’.

Soo far this is my latest piece of code for the POC, which must apply the app roles. I verified that the App Registration exists. Also the configured Managed User Identity has sufficient access:

foreach ($role in $roles) {
    $splitRole = $role.Split(";")
    $roleName = $splitRole[0]
    $roleDescription = $splitRole[1]
    Write-Host "RoleName: $($roleName)"
    Write-Host "RoleDescription: $($roleDescription)"

    $appRegistration = (Invoke-RestMethod -Method GET -Headers $headers -Uri "https://graph.microsoft.com/beta/applications/$($app.id)")
    Write-Host "App Registration: $($appRegistration)"

    # Convert the JSON output to a PowerShell object
    $appRoles = $appRegistration.appRoles
    Write-Host "App Registration Roles: $($appRoles)"

    # Extract and filter the app roles based on the DisplayName
    $filteredRoles = $appRoles | Where-Object { $_.displayName -eq $roleName }
    Write-Host "App Registration filtered roles: $($filteredRoles)"

    if ($null -eq $filteredRoles) {
        Write-Host "Role not found, create new role..."

        $newAppRole = @{
            appRoles = @(
                @{
                    allowedMemberTypes = @(
                        "Application"
                    )
                    description        = $($roleDescription)
                    displayName        = $($roleName)
                    id                 = [Guid]::NewGuid()
                    isEnabled          = $false
                    origin             = "Application"
                    value              = $($roleName)
                })
        }

        Write-Host "Updated App Registration:"
        Write-Host ($newAppRole.appRoles | Format-Table | Out-String)

        Write-Host "Updated as JSON: $($newAppRole | ConvertTo-Json)"

        try {
            $createdAppRole = Invoke-RestMethod -Method PATCH -Headers $headers -Uri "https://graph.microsoft.com/beta/applications/$($app.id)" -Body ($newAppRole | ConvertTo-Json)
            Write-Host("App Registration patch result: $($createdAppRole)")
        }
        catch {
            Write-Host "Failed to patch app roles:"
            Write-Host ($_ | Format-Table | Out-String)

            Write-Host "Other properties:"
            Write-Host ($_.message)
            Write-Host ($_.message | Format-Table | Out-String)

            Write-Host ($_.Exception.Response)
            Write-Host ($_.message)
            Write-Host ($_.message)

            $_.Exception.Response

            Write-Host "Exception details: "
            $e = $_.Exception
            Write-Host ("`tMessage: " + $e.Message)
            Write-Host ("`tStatus code: " + $e.Response.StatusCode)
            Write-Host ("`tStatus description: " + $e.Response.StatusDescription)
        }
    }
}

In my logging the JSON result of the App Roles is:

{
  "appRoles": [
    {
      "origin": "Application",
      "id": "b1d3d9dd-1cf6-4e2e-bcf1-b586a2a62244",
      "displayName": "app.customers",
      "allowedMemberTypes": "Application",
      "value": "app.customers",
      "description": "System has access to all functionalities in customer module.",
      "isEnabled": false
    }
  ]
}

I am not a very good PowerShell developer, soo maybe I am doing something wrong or overlooking something stupid;-) Hopefully somebody can help.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật