MSFT_O365User: Creating new user fails when "LicenseAssignment" array is empty
Details of the scenario you tried and the problem that is occurring
We are using Microsoft365DSC within Azure DevOps Pipelines to make sure certain break-glass accounts for our tenant are never changed or deleted. These accounts don't need any licenses by default - they'll be assigned only during the time they'll actually be used.
These accounts are being created and maintained using the MSFT_O365User resource. However, when adding a new account in the config section for that module (so the resource will create a new account), while also setting the property "LicenseAssignment" to an empty array, Set-TargetResource will fail with an error for that account. The accompanying log messages are very confusing, since they will state that 1 new license is to be assigned (should be 0) and then saying that the license "{}" couldn't be assigned. After checking the code at least for the logs showing "{}" instead of the license name I know the root cause: the log message includes the variable "$newLicenseAssignment" which was never initialized before.
The actual outcome of the whole process is correct, though: The new user account was created and has no licenses assigned. And yet our pipeline stage for applying this configuration is always coming up as failed due to that error.
Verbose logs showing the problem
2020-08-28T12:26:39.2298315Z VERBOSE: [fv-az740]: [[O365User]9a9c201c-b8ee-4ce7-8726-1d66bc4b8777] Setting configuration
2020-08-28T12:26:39.2306459Z of Office 365 User [email protected]
2020-08-28T12:26:41.6263603Z VERBOSE: [fv-az740]: [[O365User]9a9c201c-b8ee-4ce7-8726-1d66bc4b8777] Getting configuration
2020-08-28T12:26:41.6269330Z of Office 365 User [email protected]
2020-08-28T12:26:42.3324078Z VERBOSE: [fv-az740]: [[O365User]9a9c201c-b8ee-4ce7-8726-1d66bc4b8777] Getting Office 365
2020-08-28T12:26:42.3329518Z User [email protected]
2020-08-28T12:26:42.4305997Z VERBOSE: [fv-az740]: [[O365User]9a9c201c-b8ee-4ce7-8726-1d66bc4b8777] The specified User
2020-08-28T12:26:42.4311354Z {[email protected]} doesn't already exist.
2020-08-28T12:26:42.4937892Z VERBOSE: [fv-az740]: [[O365User]9a9c201c-b8ee-4ce7-8726-1d66bc4b8777] Creating Office 365
2020-08-28T12:26:42.4943402Z User [email protected]
2020-08-28T12:26:43.2321276Z VERBOSE: [fv-az740]: [[O365User]9a9c201c-b8ee-4ce7-8726-1d66bc4b8777] Assigning 1 licences
2020-08-28T12:26:43.2327324Z to new user
2020-08-28T12:26:43.3597286Z VERBOSE: [fv-az740]: [[O365User]9a9c201c-b8ee-4ce7-8726-1d66bc4b8777] Logging a new Error
2020-08-28T12:26:43.5616086Z VERBOSE: [fv-az740]: LCM: [ End Set ] [[O365User]9a9c201c-b8ee-4ce7-8726-1d66bc4b8777] in 4.3870 seconds.
2020-08-28T12:26:43.6591864Z ##[error]PowerShell DSC resource MSFT_O365User failed to execute Set-TargetResource functionality with error message: Could
not assign license {} to user {[email protected]}
+ CategoryInfo : InvalidOperation: (:) [], CimException
+ FullyQualifiedErrorId : ProviderOperationExecutionFailure
+ PSComputerName : localhost
Suggested solution to the issue
-
Change the logic for assigning licenses within Set-TargetResource of the MSFT_O365User resource so it skips license assignments if "LicenseAssignment" is an empty array or $null in the corresponding config section for users which are being newly created.
-
Update the error message to use the correct variable.
The DSC configuration that is used to reproduce the issue (as detailed as possible)
O365User 9a9c201c-b8ee-4ce7-8726-1d66bc4b8777
{
DisplayName = "XXXX";
Ensure = "Present";
FirstName = "XXXX";
GlobalAdminAccount = $Credsglobaladmin;
LastName = "XXXX";
LicenseAssignment = @();
Password = $Credsglobaladmin;
PasswordNeverExpires = $true;
UsageLocation = "DE";
UserPrincipalName = "[email protected]";
UserType = "Member";
}
The operating system the target node is running
Azure DevOps Pipeline Agent with specification "vs2017-win2016"
Version of the DSC module that was used ('dev' if using current dev branch)
1.20.805.1 (We can't install the Dev version on the agent due to already loaded modules preventing the installation of that)