【问题标题】:Powershell, filters and combining propertiesPowershell、过滤器和组合属性
【发布时间】:2013-11-26 23:42:38
【问题描述】:

我有一个 AD 组,其中有其他 AD 组作为成员。其中一些组也可能有“子组”。我想递归地遍历该组并找到有关该组用户的几个问题的答案。例如:

  • User-X 在整个组中是否“启用”?
  • User-X 的帐户是否在以下任何属性中都有值:AccountExpirationDate、accountExpires 和 Deleted?

我想要一个包含以下属性的显示结果:DisplayName、SamAccountName、AccountExpirationDate、accountExpires、Deleted 和 enabled(来自组对象)

我已尝试执行“添加成员”以从 get-ADgroupMember 插入“启用”值,但出现错误:

Add-Member : Cannot add a member with the name "enabled" because a member with that name already exists. If you want to over
write the member anyway, use the Force parameter to overwrite it.

...但据我所知,没有这样的元素。我已将 Add-Member 中的成员重命名为几个非常独特的东西,但我仍然遇到同样的错误。

我目前的尝试是:

Import-Module ActiveDirectory

get-adgroupmember -Identity "My big AD group of groups" -recursive |
    Where-Object -FilterScript {($_.ObjectClass -eq 'user')} |
    ForEach-Object {
        $enabled = $_.enebled
        Get-ADUser `
            -Filter {(name -eq $_.name)} `
            -Properties DisplayName,SamAccountName,AccountExpirationDate,accountExpires,Deleted |
            Add-Member -Name "myITGGroupEnabled" -Value $enabled -MemberType NoteProperty |
            Where-Object `
                -FilterScript {
                    ($_.AccountExpirationDate -lt [datetime]::now) `
                    -OR ($_.accountExpires -eq $true) `
                    -OR ($_.Deleted -eq $true) `
                    -OR ($_.myITGGroupEnabled -eq $false)
                }
            Select-Object DisplayName,SamAccountName,AccountExpirationDate,accountExpires,Deleted,GroupEnabled
        break
    }

我迷路了。想法?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我认为您只需要一个新的 PSObject。像这样:

        ...
        Get-AdUser -Filter {name -eq $_.name} -Properties ....  | % { 
         If  ( ($_.AccountExpirationDate -lt [datetime]::now) `
                        -OR ($_.accountExpires -eq $true) `
                        -OR ($_.Deleted -eq $true) `
                        -OR ($_.myITGGroupEnabled -eq $false)) { 
    
           New-Object PSObject -Property @{MyITGGRoupEnabled=$enabled}
         }           
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多