I'm encountering an error while creating a dynamic administrative unit in Azure using PowerShell. The script fails with the message "Invalid operands found for operator -eq", and I'm puzzled since I expected the membership rule string to work with simple variable expansion.
Below is the relevant code snippet:
foreach ($AU in $AUList) {
$Code = $AU.CountryCode
$Name = $AU.CountryName
$params = @{
displayName = "$Code" + "_Users"
description = "A dynamic administrative unit for " + "$Name"
membershipType = "Dynamic"
membershipRule = "(user.dirSyncEnabled -eq True) and (user.country -eq $Code)"
membershipRuleProcessingState = "On"
visibility = "HiddenMembership"
}
$adminUnitObj = New-MgDirectoryAdministrativeUnit -BodyParameter $params
}
The error output is as follows:
New-MgDirectoryAdministrativeUnit : Invalid operands found for operator -eq
At line:12 char:1
+ $adminUnitObj = New-MgDirectoryAdministrativeUnit -BodyParameter $par ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ({ Headers = , b...istrativeUnit }:<>f__AnonymousType1`2) [New-MgDirectoryAdministrativeUnit
_Create], Exception
+ FullyQualifiedErrorId : InvalidOperandsException,Microsoft.Graph.PowerShell.Cmdlets.NewMgDirectoryAdministrativeUnit_Create
I have tried modifying the variables and string formats, yet the error persists. From the responses, it appears that the API expects literal string values in the dynamic membership rule (e.g., the country code should be enclosed in quotes and possibly have a boolean value in lowercase).
What adjustments should I make to properly format the membership rule so that it is accepted by the API? Any assistance is appreciated.