How to count the result of a if loop within foreach loop

纵然是瞬间 提交于 2020-01-17 03:39:25

问题


Is there a better way to be able to count the amounts of empty groups than using this method and then calling $count + 1 for each result? I tried to mess around with $123.count but it kept returning 0

$Groups = Get-ADGroup -Properties * -Filter * | where { $_.Members.Count -eq 0}
$123= Foreach($G In $Groups)
{
    $Membership = Get-ADGroupMember -Identity $G.Name
    If($Membership.count -eq 0){
    $Count = $count + 1
    }

}

回答1:


Get-ADGroup can do much of what you ask without going away from AD.

Getting this done with the Filter parameter is painful. The LDAP filter however is simple.

For empty groups:

Get-ADGroup -LdapFilter "(!member=*)"

For empty groups which are also not nested inside other groups:

Get-ADGroup -LdapFilter "(&(!memberOf=*)(!member=*))"


来源:https://stackoverflow.com/questions/38998075/how-to-count-the-result-of-a-if-loop-within-foreach-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!