Skip to content

Commit 163994d

Browse files
bulk license support
1 parent d425c7c commit 163994d

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Function Invoke-ExecBulkLicense {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
Identity.User.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param (
10+
$Request,
11+
$TriggerMetadata
12+
)
13+
14+
$APIName = $TriggerMetadata.FunctionName
15+
$Results = [System.Collections.Generic.List[string]]::new()
16+
$StatusCode = [HttpStatusCode]::OK
17+
18+
try {
19+
$UserRequests = $Request.Body
20+
$TenantGroups = $UserRequests | Group-Object -Property tenantFilter
21+
22+
foreach ($TenantGroup in $TenantGroups) {
23+
$TenantFilter = $TenantGroup.Name
24+
$TenantRequests = $TenantGroup.Group
25+
$AllUsers = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?&`$select=id,userPrincipalName,assignedLicenses" -tenantid $TenantFilter
26+
$UserLookup = @{}
27+
foreach ($User in $AllUsers) {
28+
$UserLookup[$User.id] = $User
29+
}
30+
31+
# Process each user request
32+
foreach ($UserRequest in $TenantRequests) {
33+
try {
34+
$UserId = $UserRequest.userIds
35+
$User = $UserLookup[$UserId]
36+
$UserPrincipalName = $User.userPrincipalName
37+
$LicenseOperation = $UserRequest.LicenseOperation
38+
$RemoveAllLicenses = [bool]$UserRequest.RemoveAllLicenses
39+
$Licenses = $UserRequest.Licenses | ForEach-Object { $_.value }
40+
# Handle license operations
41+
if ($LicenseOperation -eq 'Add' -or $LicenseOperation -eq 'Replace') {
42+
$AddLicenses = $Licenses
43+
}
44+
45+
if ($LicenseOperation -eq 'Remove' -and $RemoveAllLicenses) {
46+
$RemoveLicenses = $User.assignedLicenses.skuId
47+
} elseif ($LicenseOperation -eq 'Remove') {
48+
$RemoveLicenses = $Licenses
49+
} elseif ($LicenseOperation -eq 'Replace') {
50+
$RemoveReplace = $User.assignedLicenses.skuId
51+
if ($RemoveReplace) { Set-CIPPUserLicense -UserId $UserId -TenantFilter $TenantFilter -RemoveLicenses $RemoveReplace }
52+
} elseif ($RemoveAllLicenses) {
53+
$RemoveLicenses = $User.assignedLicenses.skuId
54+
}
55+
#todo: Actually build bulk support into set-cippuserlicense.
56+
$TaskResults = Set-CIPPUserLicense -UserId $UserId -TenantFilter $TenantFilter -AddLicenses $AddLicenses -RemoveLicenses $RemoveLicenses
57+
58+
$Results.Add($TaskResults)
59+
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Successfully processed licenses for user $UserPrincipalName" -Sev 'Info'
60+
} catch {
61+
$ErrorMessage = Get-CippException -Exception $_
62+
$Results.Add("Failed to process licenses for user $($UserRequest.userIds). Error: $($ErrorMessage.NormalizedError)")
63+
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Failed to process licenses for user $($UserRequest.userIds). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
64+
}
65+
}
66+
}
67+
68+
$Body = @{
69+
Results = @($Results)
70+
}
71+
} catch {
72+
$ErrorMessage = Get-CippException -Exception $_
73+
$StatusCode = [HttpStatusCode]::BadRequest
74+
$Body = @{
75+
Results = @("Failed to process bulk license operation: $($ErrorMessage.NormalizedError)")
76+
}
77+
Write-LogMessage -API $APIName -message "Failed to process bulk license operation: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
78+
}
79+
80+
# Return response
81+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
82+
StatusCode = $StatusCode
83+
Body = $Body
84+
})
85+
}

0 commit comments

Comments
 (0)