Script Repository


Export group members and their membership to CSV

February 24, 2021
1385

The scripts exports group members and their membership according to a pattern to a CSV file. To run the script, use a custom command, business rule or scheduled task configured for the Group object type.

Parameters:

  • $csvFilePath - Specifies the path to the CSV file that will be created.
  • $groupNamePattern - Specifies a pattern the group names should meet. If a member of the target group is also a member of a group whose name meets the pattern, the group will be included into the CSV file
Edit Remove
PowerShell
$csvFilePath = "C:\Scripts\Members.csv" # TODO: modify me
$groupNamePattern = "View-Pool_*" # TODO: modify me

function SearchObjects ($filter, $domainName, $properties)
{
    $searcher = $Context.BindToObject("Adaxes://$domainName/RootDSE")
    $searcher.SearchFilter = $filter
    $searcher.PageSize = 500
    $searcher.SetPropertiesToLoad($properties)
    
    try
    {
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        return ,$searchResults
    }
    finally
    {
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

# Get members
try
{
    $memberGuidsBytes = $Context.TargetObject.GetEx("adm-DirectMembersGuid")
}
catch
{
    $Context.LogMessage("Group does not have members", "Warning")
    return
}

# Build filter
$filter = New-Object "System.Text.StringBuilder"
[void]$filter.Append("(|")
foreach ($guidBytes in $memberGuidsBytes)
{
    [void]$filter.Append([Softerra.Adaxes.Ldap.FilterBuilder]::Create("objectGuid", $guidBytes))
}
[void]$filter.Append(")")

# Get members info
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$searchResults = SearchObjects $filter.ToString() $domainName @("distinguishedName", "name")

$records = @()
foreach ($searchResult in $searchResults)
{
    $dn = $searchResult.Properties["distinguishedName"].Value
    
    # Search groups
    $groupSearchResults = SearchObjects "(&(objectCategory=group)(name=$groupNamePattern)(member=$dn))" $domainName @("name")
    
    # Add to report
    $record = New-Object PSObject
    $record | Add-Member -MemberType NoteProperty -Name "UserName" -Value $searchResult.Properties["name"].Value
    if ($groupSearchResults.Length -eq 0)
    {
        $record | Add-Member -MemberType NoteProperty -Name "Groups" -Value $NULL
    }
    else
    {
        $groupNames = @()
        foreach ($groupSearchResult in $groupSearchResults)
        {
            $name = $groupSearchResult.Properties["name"].Value
            $groupNames += $name
        }
        
        $groupNames = [System.String]::Join(";", $groupNames)
        $record | Add-Member -MemberType NoteProperty -Name "Groups" -Value $groupNames
    }
    $records += $record
}

# Export to CSV
$records | Export-Csv -NoTypeInformation -Path $csvFilePath


Comments ( 0 )
No results found.
Leave a comment

Related Scripts