PowerShell ile kullanıcı hesaplarına Microsoft 365 lisansları atama

Bu makale hem Microsoft 365 Kurumsal hem de Office 365 Kurumsal için geçerlidir.

Kullanıcılar, hesaplarına lisans planından lisans atanana kadar hiçbir Microsoft 365 hizmetini kullanamaz. Lisanssız hesaplara hızla lisans atamak için PowerShell'i kullanabilirsiniz.

Kullanıcı hesaplarına önce bir konum atanmalıdır. Konum belirtmek, Microsoft 365 yönetim merkezi yeni bir kullanıcı hesabı oluşturmanın gerekli bir parçasıdır.

şirket içi Active Directory Etki Alanı Hizmetleri eşitlenen hesapların varsayılan olarak belirtilen bir konumu yoktur. Bu hesaplar için şu konumlardan birini yapılandırabilirsiniz:

  • Microsoft 365 yönetim merkezi
  • PowerShell
  • Azure portal (Active Directory>Kullanıcıları> kullanıcı hesabı >Profili>kişi bilgileri>Ülke veya bölge).

Not

Microsoft 365 yönetim merkezi ile kullanıcı hesaplarına lisans atamayı öğrenin. Ek kaynakların listesi için bkz. Kullanıcıları ve grupları yönetme.

Microsoft Graph PowerShell SDK'sı ile kullanıcı hesaplarına Microsoft 365 lisansları atama

Not

Aşağıdaki betik Microsoft Graph Powershell'i kullanır. Daha fazla bilgi için bkz. Microsoft Graph PowerShell'e genel bakış.

Katılımsız betikte kimlik doğrulaması Connect-Graph yapmak için farklı yöntemler kullanma hakkında bilgi için Microsoft Graph PowerShell'de kimlik doğrulama modülü cmdlet'leri makalesine bakın.

İlk olarak Microsoft 365 kiracınıza bağlanın.

Bir kullanıcıya lisans atamak ve kaldırmak için User.ReadWrite.All izin kapsamı veya 'Lisans ata' Microsoft Graph API başvuru sayfasında listelenen diğer izinlerden biri gerekir.

Kiracıda bulunan lisansları okumak için Organization.Read.All izin kapsamı gereklidir.

Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All

Kuruluşunuzdaki Get-MgSubscribedSku her bir plandaki kullanılabilir lisans planlarını ve kullanılabilir lisans sayısını görüntülemek için komutunu çalıştırın. Her plandaki kullanılabilir lisans sayısı ActiveUnits WarningUnits - ConsumedUnits'tir - . Lisans planları, lisanslar ve hizmetler hakkında daha fazla bilgi için bkz. PowerShell ile lisansları ve hizmetleri görüntüleme.

Kuruluşunuzdaki lisanssız hesapları bulmak için bu komutu çalıştırın.

Get-MgUser -Filter 'assignedLicenses/$count eq 0' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All

Kuruluşunuzdaki lisanssız eşitlenmiş kullanıcıları bulmak için bu komutu çalıştırın.

Get-MgUser -Filter 'assignedLicenses/$count eq 0 and OnPremisesSyncEnabled eq true' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All -Select UserPrincipalName

Yalnızca UsageLocation özelliği geçerli bir ISO 3166-1 alfa-2 ülke koduna ayarlanmış kullanıcı hesaplarına lisans atayabilirsiniz. Örneğin, Birleşik Devletler için ABD ve Fransa için FR. Bazı Microsoft 365 hizmetleri belirli ülkelerde/bölgelerde kullanılamaz. Daha fazla bilgi için bkz. Lisans kısıtlamaları hakkında.

UsageLocation değeri olmayan hesapları bulmak için bu komutu çalıştırın.

Get-MgUser -Select Id,DisplayName,Mail,UserPrincipalName,UsageLocation,UserType | where { $_.UsageLocation -eq $null -and $_.UserType -eq 'Member' }

Bir hesapta UsageLocation değerini ayarlamak için bu komutu çalıştırın.

$userUPN="<user sign-in name (UPN)>"
$userLoc="<ISO 3166-1 alpha-2 country code>"

Update-MgUser -UserId $userUPN -UsageLocation $userLoc

Örneğin:

Update-MgUser -UserId "belindan@litwareinc.com" -UsageLocation US

-All parametresini kullanmadan Get-MgUser cmdlet'ini kullanırsanız, yalnızca ilk 100 hesap döndürülür.

Kullanıcı hesaplarına lisans atama

Kullanıcıya lisans atamak için PowerShell'de aşağıdaki komutu kullanın.

Set-MgUserLicense -UserId $userUPN -AddLicenses @{SkuId = "<SkuId>"} -RemoveLicenses @()

Bu örnek, lisanssız kullanıcıya belindan@litwareinc.comSPE_E5 (Microsoft 365 E5) lisans planından bir lisans atar:

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
Set-MgUserLicense -UserId "belindan@litwareinc.com" -AddLicenses @{SkuId = $e5Sku.SkuId} -RemoveLicenses @()

Bu örnekte kullanıcıya belindan@litwareinc.comSPE_E5 (Microsoft 365 E5) ve EMSPREMIUM (ENTERPRISE MOBILITY + SECURITY E5) atanır:

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
$e5EmsSku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'EMSPREMIUM'
$addLicenses = @(
    @{SkuId = $e5Sku.SkuId},
    @{SkuId = $e5EmsSku.SkuId}
)

Set-MgUserLicense -UserId "belinda@litwareinc.com" -AddLicenses $addLicenses -RemoveLicenses @()

Bu örnek, MICROSOFTBOOKINGS(Microsoft Bookings) ve LOCKBOX_ENTERPRISE (Müşteri Kasası) hizmetlerinin kapalı olduğu SPE_E5 (Microsoft 365 E5) atar:

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
$disabledPlans = $e5Sku.ServicePlans | `
    Where ServicePlanName -in ("LOCKBOX_ENTERPRISE", "MICROSOFTBOOKINGS") | `
    Select -ExpandProperty ServicePlanId

$addLicenses = @(
    @{
        SkuId = $e5Sku.SkuId
        DisabledPlans = $disabledPlans
    }
)

Set-MgUserLicense -UserId "belinda@litwareinc.com" -AddLicenses $addLicenses -RemoveLicenses @()

Bu örnek bir kullanıcıyı SPE_E5 (Microsoft 365 E5) ile güncelleştirir ve kullanıcının mevcut devre dışı planlarını geçerli durumunda bırakırken Sway ve Forms hizmet planlarını kapatır:

$userLicense = Get-MgUserLicenseDetail -UserId "belinda@litwareinc.com"
$userDisabledPlans = $userLicense.ServicePlans | `
    Where ProvisioningStatus -eq "Disabled" | `
    Select -ExpandProperty ServicePlanId

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
$newDisabledPlans = $e5Sku.ServicePlans | `
    Where ServicePlanName -in ("SWAY", "FORMS_PLAN_E5") | `
    Select -ExpandProperty ServicePlanId

$disabledPlans = ($userDisabledPlans + $newDisabledPlans) | Select -Unique

$addLicenses = @(
    @{
        SkuId = $e5Sku.SkuId
        DisabledPlans = $disabledPlans
    }
)

Set-MgUserLicense -UserId "belinda@litwareinc.com" -AddLicenses $addLicenses -RemoveLicenses @()

Bu örnek bir kullanıcıyı SPE_E5 (Microsoft 365 E5) ile güncelleştirir ve Sway ve Forms hizmet planlarını kapatırken kullanıcının mevcut devre dışı planlarını diğer tüm aboneliklerde geçerli durumunda bırakır:

$userLicense = Get-MgUserLicenseDetail -UserId belinda@litwareinc.com

$userDisabledPlans = $userLicense.ServicePlans | Where-Object ProvisioningStatus -eq "Disabled" | Select -ExpandProperty ServicePlanId

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'

$newDisabledPlans = $e5Sku.ServicePlans | Where ServicePlanName -in ("SWAY", "FORMS_PLAN_E5") | Select -ExpandProperty ServicePlanId

$disabledPlans = ($userDisabledPlans + $newDisabledPlans) | Select -Unique

$result=@()
$allPlans = $e5Sku.ServicePlans | Select -ExpandProperty ServicePlanId

foreach($disabledPlan in $disabledPlans)
{
    foreach($allPlan in $allPlans)
    {
        if($disabledPlan -eq $allPlan)
        {
            $property = @{
                Disabled = $disabledPlan
            }
        }
     }
     $result += New-Object psobject -Property $property
}


$finalDisabled = $result | Select-Object -ExpandProperty Disabled


$addLicenses = @(
    @{
        SkuId = $e5Sku.SkuId
        DisabledPlans = $finalDisabled
    }
)


Set-MgUserLicense -UserId belinda@litwareinc.com -AddLicenses $addLicenses -RemoveLicenses @()

Lisans atamasını başka bir kullanıcıdan kopyalayarak kullanıcıya lisans atama

Bu örnek, için uygulanan belindan@litwareinc.comlisanslama planının aynısını atarjamesp@litwareinc.com:

$mgUser = Get-MgUser -UserId "belindan@litwareinc.com" -Property AssignedLicenses
Set-MgUserLicense -UserId "jamesp@litwareinc.com" -AddLicenses $mgUser.AssignedLicenses -RemoveLicenses @()

Kullanıcıyı farklı bir aboneliğe taşıma (lisans planı)

Bu örnek, kullanıcıyı SPE_E3 (Microsoft 365 E3) lisans planından SPE_E5 (Microsoft 365 E5) lisans planına yükseltmektedir:

$e3Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E3'
$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'

# Unassign E3
Set-MgUserLicense -UserId "belindan@litwareinc.com" -AddLicenses @{} -RemoveLicenses @($e3Sku.SkuId)
# Assign E5
Set-MgUserLicense -UserId "belindan@litwareinc.com" -AddLicenses @{SkuId = $e5Sku.SkuId} -RemoveLicenses @()

Bu komutla kullanıcı hesabı için abonelikteki değişikliği doğrulayabilirsiniz.

Get-MgUserLicenseDetail -UserId "belindan@litwareinc.com"

Ayrıca bkz.

PowerShell ile Microsoft 365’i yönetme

PowerShell ile Microsoft 365’i yönetme

Microsoft Graph PowerShell SDK'sını kullanmaya başlama

Microsoft Graph kullanıcısını kullanma: assignLicense ve subscribedSku API'leri