small script I wrote to set users 365 licenses based on Active Directory group membership, only tested in a hybrid environment.

function grantGroupLicense($group, $license){
    $members = Get-ADGroupMember $group -Recursive
    
    foreach($member in $members){
        $adUser = get-aduser $member.samaccountname | select userprincipalname
        $msolUser = Get-MsolUser -UserPrincipalName $adUser.userPrincipalName
        grantLicense $msolUser $license
    }

    function checkLicense($user, $license){
        foreach($lic in $user.licenses){
            if($lic.AccountSku.SkuPartNumber -eq $license){return $true}
        }
        return $false
    }

    function grantLicense($msolUser, $license){
        if(-not(checkLicense $msolUser $license)){
            try{
                set-msolUserLicense -userprincipalname $msolUser.userPrincipalName -AddLicenses $license -ErrorAction Stop
                write-host $msolUser.userPrincipalName Has been given $license -ForegroundColor green
            }catch{
                write-host $msolUser.userPrincipalName could not be given $license -ForegroundColor red
            }
        }else{
            write-host $msolUser.userPrincipalName already has $license -ForegroundColor green
        }
    }
}

Usage:
grantGroupLicense <AD-Group-Name> <License SkuPartNumber>

eg:
grantGroupLicense FG-MSVisio_Users VISIOCLIENT
grantGroupLicense FG-MSProject_Users PROJECTCLIENT