Powershell AD:计算办公室中的组

时间:2018-09-26 16:25:46

标签: powershell count active-directory formatting

我在思考过程中遇到问题。我想要类似的输出 “组名”,“标题”,“计数”,我能够获得“组名”和“标题”列,但是我不确定如何计算“销售”标题中的所有重复组。我的代码现在在哪里,正在搜索整个列以查看是否找到了该组名,是否找到了该列,然后将计数器移动了1(这是错误的,因为我找到了一个具有不同标题的组名)

任何帮助将不胜感激!谢谢

$allOffices = get-aduser -filter * -Properties physicaldeliveryofficename | Select-Object -ExpandProperty physicaldeliveryofficename #Selecting all offices
$uniOffices = $allOffices | Select-Object -Unique
$groups = $null
$global:ErrorActionPreference = 'silentlycontinue'
$finallist = @()
$count = 0


foreach ($office in $uniOffices) {   # Loop through all unique offices
    $peopleInOffice = $null
    $peopleInOffice = get-aduser -filter * -properties office | Where-Object office -eq $office | Select-Object -ExpandProperty SamAccountName  # Array of people in a certain office, selects username
    foreach ($person in $peopleInOffice){
        $groups = get-ADPrincipalGroupMembership $person
        foreach($group in $groups) {
                if(($finallist.'Group Name'-contains $group.name) -and ($finallist.'Title' -contains $office)){$count++}
                $finallist+=  $group| Select-Object @{N='Group Name';E={$group.name}},@{N='Title';E={($office)}},@{N='Count';E={$count}}
        }
    }
}

$finallist

1 个答案:

答案 0 :(得分:0)

因此,我开始着手解决您的问题,但不确定您要问什么。这是代码的极大简化和更有效的表示形式:

$offices = Get-ADUser -Filter 'PhysicalDeliveryOfficeName -like "*"' -Properties PhysicalDeliveryOfficeName |
    Select-Object -ExpandProperty PhysicalDeliveryOfficeName -Unique

$list = foreach ($office in $offices)
{
    foreach ($person in (Get-ADUser -Filter "Office -eq '$office'" -Properties Office, MemberOf))
    {
        $person.MemberOf |
            Get-ADGroup -Properties Name |
            Select-Object -Property @(
                @{
                    N = 'Group Name'
                    E = {$PSItem.Name}
                }
                @{
                    N = 'Title'
                    E = {$office}
                }
            )
    }
}

最后,您有一个$list对象,它是可以操纵的pscustomobject对象的数组。您可以使用Group-Object通过计算出的属性等来确定有多少个名称相似的组。


使用问题陈述:

  

如何计算sales标题中的所有重复组

$list | Group-Object -Property Title | Select-Object -ExpandProperty Count