如何计算foreach循环中if循环的结果 [英] How to count the result of a if loop within foreach loop

查看:167
本文介绍了如何计算foreach循环中if循环的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方法来计算空组的数量比使用这种方法,然后调用每个结果$ count + 1?我试图搞乱 $ 123.count 但是它一直返回0

  $ Groups = Get-ADGroup -Properties * -Filter * | $ $ $ $ $ $ $ $ = $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b如果($ Membership.count -eq 0){
$ Count = $ count + 1
}

}
解决方案Get-ADGroup可以在不脱离AD的情况下完成所要求的任务。

解决方案


使用Filter参数完成此操作是痛苦的。 LDAP过滤器很简单。



对于空组:

  Get-ADGroup -LdapFilter(!member = *)

对于也不是嵌套的空组在其他组中:

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


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
    }

}

解决方案

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=*))"

这篇关于如何计算foreach循环中if循环的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆