2013-10-03 62 views
0

我有一個AD組,其他AD組作爲成員。其中一些小組也可能有「小組」。我想通過這個組遞歸下降,並找到有關該組中用戶的幾個問題的答案。例如:Powershell,過濾器和組合屬性

  • User-X在整個組中是否「啓用」?
  • User-X的帳戶是否在任何屬性中都有值:AccountExpirationDate,accountExpires和Deleted?

我想,它包含的屬性所顯示的結果是:顯示名稱,SAM帳戶名,AccountExpirationDate,accountExpires,刪除,啓用(從組對象)

我試圖做一個「添加會員」插入從獲得-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 
    } 

我迷路了。想法?

回答

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} 
    }   
    }