代码之家  ›  专栏  ›  技术社区  ›  BPengu

发现运算符-eq的操作数无效

  •  3
  • BPengu  · 技术社区  · 4 月前

    我对这个powershell错误感到困惑,因为我认为这是一个字符串,实际内容应该无关紧要。

    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
        }
    

    导致以下错误

    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
    

    我不确定我的成员规则的哪一部分会打乱它,但我尝试过将它们设置为可用变量和字符串。有人能帮我找出它有什么问题吗?

    1 回复  |  直到 4 月前
        1
  •  3
  •   Santiago Squarzon    4 月前

    你的动态过滤器有两个明显的问题,其中一个我不太确定(如果我错了,请纠正我),但如果我没记错的话,布尔值是区分大小写的,所以应该是 true 而不是 True 另一个问题是 country 缺少引号,如 shown in the docs :

    物业 允许值 使用方法
    国家 任何字符串值或null user.country -eq "value"

    因此,要解决这个问题,您应该将过滤器更改为:

    membershipRule = "(user.dirSyncEnabled -eq true) and (user.country -eq `"$Code`")"
    

    因为你正在使用 expandable string "..." ,逃离内心 " 你可以使用倒勾( ` ).

    另一种可能更易读、更容易理解的选择是通过以下方式使用字符串插值 -f operator ,在这种情况下,您可以在外部使用单引号:

    membershipRule = '(user.dirSyncEnabled -eq true) and (user.country -eq "{0}")' -f $Code
    
    推荐文章