Operation returned an invalid status code 'BadRequest'

I am creating groups to assign privileges to the subscriptions in Azure.
The Groups are created as intended, however the role assignment fails:

        $GroupName = $Prefix + $Name + "-" + $RoleName
        $Desc = "Subscription $($Sub.Name) $($Role)"
        Write-Host "Creating Group $($GroupName) in AzureAD" -ForegroundColor Green
        New-AzADGroup -MailEnabled:$False -MailNickName $GroupName -displayName $GroupName -SecurityEnabled:$true -Description $Desc
        $CreatedGroup = Get-AZADGroup -Filter "DisplayName eq '$GroupName'"
        $Scope = "/subscriptions/" + $Sub.id
        $Roleid = Get-AzRoleDefinition -Name $Role
        New-AZRoleAssignment -ObjectID $CreatedGroup.id -RoleDefinitionId $RoleID.id -Scope $Scope

Failure

New-AzRoleAssignment:
Line |
  30 |          New-AZRoleAssignment -ObjectID $CreatedGroup.id -RoleDefiniti …
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Operation returned an invalid status code 'BadRequest'

However when i run the command outside the script, it works:

New-AZRoleAssignment -ObjectID $CreatedGroup.id -RoleDefinitionId $RoleID.id -Scope $Scope

So the valiues defined are correct.
I have searched around, and New-AZRoleAssignment does seem to to trigger this event, yet i was unable to find the root cause. Are there other ways to achieve this goal?

Hello @Killerbe,
What role are trying to assign?
In your snippet you are getting role definition for $Role but it is not specified (at least in the piece of code you provided).

Could you share the rest of your code?

This the compleet script

Connect-AzAccount
# Roles
$Roles = "Reader", "Contributor", "Owner", "Security Admin"
########################
$Subs = Get-AzSubscription | Where {$_.State -eq "Enabled"}
foreach ($Sub in $Subs) {
    # Creating Azure AD Groups
    # Creating Subscription Reader
    # Naming
    $Prefix = "AZU-SUB-"
    $Name = $Sub.Name
    If ($Name.Contains(" ")) {
        Write-Warning "The Name $($Name) contains spaces, these will be replace by underscores."
        $Name = $Name -Replace (" ", "_")
    }
    else {
        Write-Host "the Name value does not contains any unallowed characters" -ForegroundColor Green
    }
    # Creating groups
    Foreach ($Role in $Roles) {
        If ($role -eq "Security Admin") {
            $RoleName = "SecAdm"
        }
        Else {
            $RoleName = $Role
        }
        $GroupName = $Prefix + $Name + "-" + $RoleName
        $Desc = "Subscription $($Sub.Name) $($Role)"
        Write-Host "Creating Group $($GroupName) in AzureAD" -ForegroundColor Green
        New-AzADGroup -MailEnabled:$False -MailNickName $GroupName -displayName $GroupName -SecurityEnabled:$true -Description $Desc
        Start-Sleep 5
        $CreatedGroup = Get-AZADGroup -Filter "DisplayName eq '$GroupName'"
        $Scope = "/subscriptions/" + $Sub.id
        $Roleid = Get-AzRoleDefinition -Name $Role
        New-AZRoleAssignment -ObjectID $CreatedGroup.id -RoleDefinitionId $RoleID.id -Scope $Scope
        Pause
    }
}