#在Office365中,有两种分配许可的方式:
#1.使用admin.microsoft.com直接分配(这样批量操作起来想多繁琐),许可类型在Azure AD的许可中心显示为Direct
#2.使用portal.azure.com的group based licensing方法(自动根据组员将许可分配给用户),相关文档: https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/licensing-groups-assign ,许可类型在Azure AD的许可中心显示为Inherited

#Office 365中会有部分用户因为管理员使用了以上两种方式分配许可证,导致用户同样的许可证被分配了两次,许可类型在Azure AD的许可中心显示为Direct&Inherited。目前portal.azure.com没有提供一键移除的方法,以下PowerShell脚本可以达成一键移除的功能。

#In Office365, there are two ways to assign licenses:
#1. Use admin.microsoft.com to assign directly (so the batch operation is too cumbersome), the license type is displayed as Direct in the license center of Azure AD
#2. Use the group based licensing method of portal.azure.com (automatically assign licenses to users based on group members), related documents: https://docs.microsoft.com/en-us/azure/active-directory/users -groups-roles/licensing-groups-assign, the license type is displayed as Inherited in the license center of Azure AD

#There will be some users in #Office 365 because the administrator uses the above two methods to allocate licenses, resulting in the same license being allocated twice for users. The license type is displayed as Direct&Inherited in the license center of Azure AD. Currently portal.azure.com does not provide a one-click removal method. The following PowerShell script can achieve the one-click removal function.

#Script start

function Users-LicenseType
{
    Param(
    [System.Management.Automation.PSCredential]$cred
         )
    Connect-MsolService -Credential $cred

    $Gplist= @{}
    $Group =Get-msolgroup
    $licenses =  Get-MsolAccountSku
    #Get all groupname with group objectId
    foreach($gp in $Group)
    {
    $Gplist+=@{$gp.ObjectId.ToString() = $gp.DisplayName}
    }
    $users= Get-MsolUser -All
    $AllUser = @()

    Foreach($license in $licenses)
    {
        foreach($user in $users)
        {   
            # Find Users License Type 
            $UserList = "" | Select-Object "License","UserPrincipalName","LicenseType"
            $Assigneds=$user.Licenses
            $status = ""

            foreach($assigned in $assigneds)
            {
                If($license.accountskuid -eq $assigned.accountskuid)
                {

                    $lic = $user.Licenses.GroupsAssigningLicense.Guid
                    if($lic -ne $null)
                    {
                    $GpName = ''
                    foreach($lc in $lic)
                    {
                        If($GpName) {
                                    if($Gplist.Item($lc.ToString()) -ne $null)
                                    {
                                    $GpName=$GpName + ";" + $Gplist.Item($lc.ToString())
                                    }
                                } 
                            Else {
                                    if($Gplist.Item($lc.ToString()) -ne $null)
                                    {
                                    $GpName=$Gplist.Item($lc.ToString())
                                    }
                                }          
                    }
                    foreach($lc in $lic)
                    {
                        If(Get-MsolUser -objectid $lc -ErrorAction SilentlyContinue)
                        {
                            $status = "Direct&Inherited("+$GpName+")"
                        }

                    }
                    $UserList.UserPrincipalName = $user.UserPrincipalName
                    If($status)
                    {
                        $UserList.LicenseType = $status

                    }else{

                        $UserList.LicenseType = "Inherited("+$GpName+")"

                    }
                    $UserList.License = $assigned.accountskuid
                    $AllUser+= $UserList
                    $UserList =$null

                    }Else{

                    $UserList.UserPrincipalName = $user.UserPrincipalName
                    $UserList.LicenseType = "Direct"
                    $UserList.License = $assigned.accountskuid
                    $AllUser+= $UserList
                    $UserList =$null

                    }
                }

            If($status -like "*Direct&Inherited*")
            {

                Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicenses $Assigned.accountskuid

            }

            }

        }
    }

    $AllUser  | Sort-Object -Property License
}

$cred =Get-Credential

$Listofusers = Users-LicenseType -cred $cred

$Listofusers