Powershell,过滤器和组合属性

时间:2013-10-03 20:03:10

标签: powershell

我有一个AD组,其他AD组作为成员。其中一些群体也可能有“子群体”。我想以递归的方式通过这个组,找到关于该组中用户的几个问题的答案。例如:

  • 整个群组中是否启用了User-X?
  • User-X的帐户是否包含任何属性中的值:AccountExpirationDate,accountExpires和Deleted?

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

我尝试过“添加成员”来插入get-ADgroupMember中的“enabled”值,但是我收到错误:

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
    }

我迷路了。想法?

1 个答案:

答案 0 :(得分:0)

我认为你需要的只是一个新的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}
     }           
    }