接受我可以使用和更正的代码的例子
我目前的问题是如何计算组中的成员数量而不是打印出组中的所有成员(包括其ID名称或PC名称)。注释掉的代码打印每个成员。我只想数数。
我在foreach循环中尝试了$ members.count,$ member.count和$ string.count但没有打印出来。请帮忙
Import-Module ActiveDirectory
$pathEmpty = "C:\Temp\groupsEmpty.txt"
Clear-Content $pathEmpty
$Header = `
"Group ID Name" + "|" + `
"Display Name" + "|" + `
"Description" + "|" + `
"Members"
#Write out the header
$Header | Out-File $pathEmpty -Append
$emptys = get-adgroup -properties name, displayname, description, members -filter name -like "WA*" -or name -like "workstation*"} `
| Select name, displayname, description, members
foreach ($empty in $emptys)
{
#clears previous
$members = ""
foreach ($member in $empty.members)
{
$string = $member.substring(3,$member.indexof(",")-3)
#$members = $members + ":" + $string
$string.count
}
$listing =`
$empty.Name + "|" + `
$empty.DisplayName + "|" + `
$empty.Description + "|" + `
$members
$listing | Out-File $pathEmpty -Append
}
我根据Alex McKenzie的评论进行了编辑。你给我的最初的例子提出了问题。 我尝试添加一些编辑但是我收到一个错误:无法验证参数'Identity'的参数。参数为null或空。提供一个非null或空的参数然后尝试 命令再次。
foreach ($empty in $emptys)
{
#clears previous
$members = ""
foreach ($member in $empty.members)
{
#$string = $member.substring(3,$member.indexof(",")-3)
#$members = $members + ":" + $string
if($member -eq $null){
$users = 0
}
else{
$users = (Get-ADGroupMember -Identity $($_.DistinguishedName)).Count
#$users.count
}
}
以下是我完成的代码。我只为我需要的每件事制作了单独的.txt文件,并将它们分别导入excel。 问题解决
Import-Module ActiveDirectory
##########################################################################
#This Section involves filtering WA groups and retriving their information
#In the info section (aka Notes section) is very important to look
#over if groups require approvals or not
##########################################################################
$pathAll = "C:\Temp\groupsEALL.txt"
Clear-Content $pathALL
$Header = `
"Group ID Name" + "|" + `
"Description" + "|" + `
"Notes Field" + " |" + "Members"
#Write out the header
$Header | Out-File $pathALL -Append
$emptys = get-adgroup -properties name, description, members, info -filter {name -like "WA*" -or name -like "workstation*"} `
| Select name, description, members, info
foreach ($empty in $emptys)
{
#clears previous
$members = ""
foreach ($member in $empty.members)
{
#$users = (Get-ADGroupMember -Identity $member.DistinguishedName).Count
$string = $member.substring(3,$member.indexof(",")-3)
$members = $members + ":" + $string
}
$listing =`
$empty.Name + "|" + `
$empty.Description + "|" + `
$empty.Info + "|" + `
$members
$listing | Out-File $pathALL -Append
}
################################################################
#This next section will determine the member count in the groups
################################################################
$pathCount = "C:\Temp\Groupcount.txt"
Clear-Content $pathcount
$groups = Get-ADGroup -filter {(name -like "WA*") -or (name -like "workstation*")}
foreach($group in $groups){
#initializing
$countUser = ""
$countUser = ((get-Adgroup $group -properties members).members).count
"The group $($group.Name) has $countUser user(s)." | Out-File $pathCount -Append
}
我在原始脚本中添加一个计数器,如果我想让它计数
foreach ($empty in $emptys)
{
#clears previous
$members = ""
#initialize member counting
$count = 0
foreach ($member in $empty.members)
{
#counts how many members
$count += 1
$countSum = "The group $($group.Name) has $count user(s)."
$string = $member.substring(3,$member.indexof(",")-3)
$members = $members + ":" + $string
}
$listing =`
$empty.Name + "|" + `
$empty.Description + "|" + `
#$empty.Info + "|" + `
$empty.info + "|" + `
$members + "|" + $countSum
$listing | Out-File $pathALL -Append
}
答案 0 :(得分:12)
`Get-ADGroupMember' cmdlet会以比你绑定更有效的方式解决这个问题。
举个例子:
$users = Get-ADGroupMember -Identity 'Group Name'
$users.count
132
编辑:
为了澄清事情,并使您的脚本更简单。这是一个适用于您的环境的通用脚本,可输出与您的过滤器匹配的每个组的用户数。
$groups = Get-ADGroup -filter {(name -like "WA*") -or (name -like "workstation*")}
foreach($group in $groups){
$countUser = (Get-ADGroupMember $group.DistinguishedName).count
Write-Host "The group $($group.Name) has $countUser user(s)."
}
答案 1 :(得分:3)
每个回复都错过了一个细节。如果该组只有1个用户该怎么办。
$count = @(get-adgroupmember $group).count
将命令放在@()包装器中,以便结果是一个数组,无论如何,所以即使它是1项,你也会得到一个计数。
答案 2 :(得分:2)
简单的方法: 要获得实际用户数:
$ADInfo = Get-ADGroup -Identity '<groupname>' -Properties Members
$AdInfo.Members.Count
你可以轻松获得数量,对于20k +用户来说也很快
答案 3 :(得分:1)
如果您无法使用ActiveDirectory
模块或Get-ADGroupMember
cmdlet,则可以使用LDAP&#34;在链&#34;匹配规则中执行此操作:
$GroupDN = "CN=MyGroup,OU=Groups,DC=mydomain,DC=tld"
$LDAPFilter = "(&(objectClass=user)(objectCategory=Person)(memberOf:1.2.840.113556.1.4.1941:=$GroupDN))"
# Ideally using an instance of adsisearcher here:
Get-ADObject -LDAPFilter $LDAPFilter
有关在Active Directory中实施的其他LDAP匹配规则,请参阅MSDN
答案 4 :(得分:0)
在Powershell中,您需要导入活动目录模块,然后使用get-adgroupmember,然后使用measure-object。例如,要获取属于该组&#34;域用户&#34;的用户数,请执行以下操作:
Import-Module activedirecotry
Get-ADGroupMember "domain users" | Measure-Object
在&#34; Get-ADGroupMember&#34;之后输入组名称时,如果名称是没有空格的单个字符串,则不需要引号。如果组名中包含空格,请使用它周围的引号。
输出结果如下:
Count : 12345
Average :
Sum :
Maximum :
Minimum :
Property :
注意 - 如果您已经将PowerShell用于其他AD管理任务,则导入活动目录模块可能会是多余的。
答案 5 :(得分:0)
我想分享的东西..
$adinfo.members
实际上给出了实际成员数量的两倍。 $adinfo.member
(没有“s”)会返回正确的金额。即使在倾销$adinfo.members
&amp; $adinfo.member
屏蔽输出较少量的成员。
不知道如何解释这个!
答案 6 :(得分:0)
这个怎么样?
Get-ADGroupMember 'Group name' | measure-object | select count
答案 7 :(得分:0)
$users = Get-ADGroupMember -Identity 'Client Services' -Recursive ; $users.count
上面的单行提供了一个干净的递归成员计数。简单,简单,一行。
答案 8 :(得分:0)
尝试:
$group = Get-ADGroup -Identity your-group-name -Properties *
$group.members | count
这对我有一个拥有17000多个成员的小组有用。