-
Notifications
You must be signed in to change notification settings - Fork 4
/
remove-msCommunicationLicensesFromM365E3.ps1
43 lines (37 loc) · 1.62 KB
/
remove-msCommunicationLicensesFromM365E3.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Connect-Graph -Scopes User.ReadWrite.All, Organization.Read.All
$users = Get-MgUser -All
$sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E3'
foreach($user in $users){
try{
$userLicense = $Null; $userLicense = Get-MgUserLicenseDetail -UserId $user.id | Where {$_.SkuPartNumber -eq 'SPE_E3'}
if(!$userLicense){
write-Host "$($user.userPrincipalName) skipping because not M365 E3"
continue
}
[Array]$userDisabledPlans = @($userLicense.ServicePlans | Where ProvisioningStatus -eq "Disabled" | Select -ExpandProperty ServicePlanId)
$update = $False
if($userDisabledPlans -notcontains "57ff2da0-773e-42df-b2af-ffb7a2317929"){
$userDisabledPlans += "57ff2da0-773e-42df-b2af-ffb7a2317929"
$update = $true
}
if($userDisabledPlans -notcontains "0feaeb32-d00e-4d66-bd5a-43b5b83db82c"){
$userDisabledPlans += "0feaeb32-d00e-4d66-bd5a-43b5b83db82c"
$update = $True
}
if($update){
write-host "$($user.userPrincipalName) updating "
$addLicenses = @(
@{
SkuId = $sku.SkuId
DisabledPlans = $userDisabledPlans
}
)
$res = Set-MgUserLicense -UserId $user.id -AddLicenses $addLicenses -RemoveLicenses @()
write-host "$($user.userPrincipalName) updated "
}else{
write-Host "$($user.userPrincipalName) skipping because already disabled"
}
}catch{
write-Host "$($user.userPrincipalName) FAILED: $_" -ForegroundColor Red
}
}