-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSLicenseAssign.ps1
53 lines (49 loc) · 2.39 KB
/
MSLicenseAssign.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
44
45
46
47
48
49
50
51
52
53
<# Script to License Students and Employees in O365
Created by J. Carlton Bryan
05/30/2023
added check for installed modules => 9/19/2023
#>
$MsGraphModule = Get-Module Microsoft.Graph -ListAvailable
if($null -eq $MsGraphModule)
{
Write-host "Important: Microsoft graph module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
$confirm = Read-Host Are you sure you want to install Microsoft graph module? [Y] Yes [N] No
if($confirm -match "[yY]")
{
Write-host "Installing Microsoft graph module..."
Install-Module Microsoft.Graph -AllowClobber -Force
Import-Module Microsoft.Graph.Users, Microsoft.Graph.Users.Actions
Write-host "Microsoft graph module is installed in the machine successfully" -ForegroundColor Magenta
}
else
{
Write-host "Exiting. `Note: Microsoft graph module must be available in your system to run the script" -ForegroundColor Red
Exit
}
}
Connect-MgGraph -scope User.ReadWrite.All, Organization.Read.All -ErrorAction SilentlyContinue -Errorvariable ConnectionError |Out-Null
if($ConnectionError -ne $null)
{
Write-Host "$ConnectionError" -Foregroundcolor Red
Exit
}
$unlicensedUsersJCB = $null
$unlicensedUsersJCB = Get-MgUser -Filter 'assignedLicenses/$count eq 0 and OnPremisesSyncEnabled eq true and accountEnabled eq true' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All -Select Mail, UserPrincipalName, ID
$license1 = Get-MgSubscribedSku | Where-Object {$_.SkuPartNumber -eq "SKU License Number"}
$license2 = Get-MgSubscribedSku | Where-Object {$_.SkuPartNumber -eq "SKU License Number"}
ForEach ($user in $unlicensedUsersJCB) # cycle through unlicensed user
{
If ($user.Mail) # Has email - assign License
{
if ($user.Mail -match "@student.email") # Student
{
Update-MgUser -UserId $user.Id -UsageLocation US
Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = ($license1.SkuId)} -RemoveLicenses @()
} # end student section
if ($user.Mail -match "@employee.email") # Employee
{
Update-MgUser -UserId $user.Id -UsageLocation US
Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = ($license2.SkuId)} -RemoveLicenses @()
} # end employee section
} # end if - assign License
} # end ForEach