Skip to content

Commit a72cd80

Browse files
authored
Merge pull request KelvinTegelaar#1657 from kris6673/fix-usagelocation-null
Fix: Usage location fallback and enhance logging in Set-CIPPUserLicense
2 parents 4638918 + 8053100 commit a72cd80

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Users/Invoke-AddUserBulk.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function Invoke-AddUserBulk {
128128
if ($AssignedLicenses) {
129129
$GuidPattern = '([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})'
130130
$LicenseSkus = $AssignedLicenses.value ?? $AssignedLicenses | Where-Object { $_ -match $GuidPattern }
131-
Set-CIPPUserLicense -UserId $BulkResult.id -AddLicenses $LicenseSkus -TenantFilter $TenantFilter
131+
Set-CIPPUserLicense -UserId $BulkResult.id -AddLicenses $LicenseSkus -TenantFilter $TenantFilter -APIName $APIName -Headers $Headers
132132
}
133133
$Results.Add(@{
134134
resultText = $Message.resultText

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Users/Invoke-EditUser.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ function Invoke-EditUser {
126126
$Results.Add( 'Success. User license is already correct.' )
127127
} else {
128128
if ($UserObj.removeLicenses) {
129-
$licResults = Set-CIPPUserLicense -UserId $UserObj.id -TenantFilter $UserObj.tenantFilter -RemoveLicenses $CurrentLicenses.assignedLicenses.skuId -Headers $Headers
129+
$licResults = Set-CIPPUserLicense -UserId $UserObj.id -TenantFilter $UserObj.tenantFilter -RemoveLicenses $CurrentLicenses.assignedLicenses.skuId -Headers $Headers -APIName $APIName
130130
$Results.Add($licResults)
131131
} else {
132132
#Remove all objects from $CurrentLicenses.assignedLicenses.skuId that are in $licenses
133133
$RemoveLicenses = $CurrentLicenses.assignedLicenses.skuId | Where-Object { $_ -notin $licenses }
134-
$licResults = Set-CIPPUserLicense -UserId $UserObj.id -TenantFilter $UserObj.tenantFilter -RemoveLicenses $RemoveLicenses -AddLicenses $licenses -Headers $headers
134+
$licResults = Set-CIPPUserLicense -UserId $UserObj.id -TenantFilter $UserObj.tenantFilter -RemoveLicenses $RemoveLicenses -AddLicenses $licenses -Headers $Headers -APIName $APIName
135135
$Results.Add($licResults)
136136
}
137137

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Users/Invoke-ExecBulkLicense.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Invoke-ExecBulkLicense {
1+
function Invoke-ExecBulkLicense {
22
<#
33
.FUNCTIONALITY
44
Entrypoint
@@ -11,7 +11,8 @@ Function Invoke-ExecBulkLicense {
1111
$TriggerMetadata
1212
)
1313

14-
$APIName = $TriggerMetadata.FunctionName
14+
$APIName = $Request.Params.CIPPEndpoint
15+
$Headers = $Request.Headers
1516
$Results = [System.Collections.Generic.List[string]]::new()
1617
$StatusCode = [HttpStatusCode]::OK
1718

@@ -48,12 +49,12 @@ Function Invoke-ExecBulkLicense {
4849
$RemoveLicenses = $Licenses
4950
} elseif ($LicenseOperation -eq 'Replace') {
5051
$RemoveReplace = $User.assignedLicenses.skuId
51-
if ($RemoveReplace) { Set-CIPPUserLicense -UserId $UserId -TenantFilter $TenantFilter -RemoveLicenses $RemoveReplace }
52+
if ($RemoveReplace) { Set-CIPPUserLicense -UserId $UserId -TenantFilter $TenantFilter -RemoveLicenses $RemoveReplace -APIName $APIName -Headers $Headers }
5253
} elseif ($RemoveAllLicenses) {
5354
$RemoveLicenses = $User.assignedLicenses.skuId
5455
}
5556
#todo: Actually build bulk support into set-cippuserlicense.
56-
$TaskResults = Set-CIPPUserLicense -UserId $UserId -TenantFilter $TenantFilter -AddLicenses $AddLicenses -RemoveLicenses $RemoveLicenses
57+
$TaskResults = Set-CIPPUserLicense -UserId $UserId -TenantFilter $TenantFilter -AddLicenses $AddLicenses -RemoveLicenses $RemoveLicenses -APIName $APIName -Headers $Headers
5758

5859
$Results.Add($TaskResults)
5960
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Successfully processed licenses for user $UserPrincipalName" -Sev 'Info'

Modules/CIPPCore/Public/Set-CIPPUserLicense.ps1

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ function Set-CIPPUserLicense {
55
[Parameter(Mandatory)][string]$TenantFilter,
66
[Parameter()][array]$AddLicenses = @(),
77
[Parameter()][array]$RemoveLicenses = @(),
8-
$Headers
8+
$Headers,
9+
$APIName = 'Set User License'
910
)
1011

1112
# Build the addLicenses array
@@ -28,7 +29,25 @@ function Set-CIPPUserLicense {
2829
Write-Host "License body JSON: $LicenseBodyJson"
2930

3031
try {
31-
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/users/$UserId/assignLicense" -tenantid $TenantFilter -type POST -body $LicenseBodyJson -Verbose
32+
try {
33+
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/users/$UserId/assignLicense" -tenantid $TenantFilter -type POST -body $LicenseBodyJson -Verbose
34+
} catch {
35+
# Handle if the error is due to missing usage location
36+
if ($_.Exception.Message -like '*invalid usage location*') {
37+
$Table = Get-CippTable -tablename 'UserSettings'
38+
$UserSettings = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'UserSettings' and RowKey eq 'allUsers'"
39+
if ($UserSettings) { $DefaultUsageLocation = (ConvertFrom-Json $UserSettings.JSON -Depth 5 -ErrorAction SilentlyContinue).usageLocation.value }
40+
$DefaultUsageLocation ??= 'US' # Fallback to US if not set
41+
42+
$UsageLocationJson = ConvertTo-Json -InputObject @{'usageLocation' = $DefaultUsageLocation } -Depth 5 -Compress
43+
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/users/$UserId" -tenantid $TenantFilter -type PATCH -body $UsageLocationJson -Verbose
44+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message "Set usage location for user $UserId to $DefaultUsageLocation" -Sev 'Info'
45+
# Retry assigning the license
46+
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/users/$UserId/assignLicense" -tenantid $TenantFilter -type POST -body $LicenseBodyJson -Verbose
47+
} else {
48+
throw $_
49+
}
50+
}
3251
} catch {
3352
$ErrorMessage = Get-CippException -Exception $_
3453
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message "Failed to assign the license. Error: $($ErrorMessage.NormalizedError)" -Sev Error -LogData $ErrorMessage

0 commit comments

Comments
 (0)