The script exports a list of members of an AD group to a CSV file. The list includes both direct and indirect group members.
Parameter:
- $csvFilePath - Specifies a full path to the CSV file that will be created by the script. In the path, the {0} placeholder will be replaced with the date and time of the export. You can use value references to specify a path. They will be replaced with corresponding property values of the group whose memberships are exported. For example, if you specify %name%, this text will be replaced with the group name.
PowerShell
$csvFilePath = "\\server\share\%name%_MembershipReport-{0}.csv" # TODO: Modify me
$now = [System.DateTime]::Now.ToString("yyyy.MM.dd.HH.mm.ss")
$csvFilePath = [System.String]::Format($csvFilePath, $now)
# Get group members
try
{
$memberGuidsBytes = $Context.TargetObject.GetEx("adm-MembersGuid")
}
catch
{
$Context.LogMessage("The group has no members. No CSV file will be created.", "Warning")
return # Exit script
}
# Build report
$report = @()
foreach ($memberGuidBytes in $memberGuidsBytes)
{
# Bind to the group member
$memberGuid = New-Object "System.Guid" (, $memberGuidBytes)
$memberGuid = $memberGuid.ToString("B")
$memberPath = "Adaxes://<GUID=$memberGuid>"
$member = $Context.BindToObject($memberPath)
# Add member information to the report
$memberName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($member, 'IncludeParentPath')
if ($Context.TargetObject.IsMember($memberPath))
{
$membershipType = "Direct"
}
else
{
$membershipType = "Indirect"
}
$memberClass = $member.Class
$reportEntry = New-Object PSObject
$reportEntry | Add-Member -Name Name -Value $memberName -MemberType NoteProperty
$reportEntry | Add-Member -Name Class -Value $memberClass -MemberType NoteProperty
$reportEntry | Add-Member -Name "Membership Type" -Value $membershipType -MemberType NoteProperty
$report += $reportEntry
}
# Export report to CSV
$report | Export-Csv -Path $csvFilePath -NoTypeInformation