Script Repository


Members of groups based on property value

June 06, 2022
45

The script generates a report containing members of the groups that have a property with a specific value. To execute the script, create a report.

Parameters:

  • $namingParameter - Specifies the name of the parameter used to enter the value to search for. The parameter name should contain the param- prefix.
  • $searchPropertyParameter - Specifies the name of the parameter of the Property picker type used to select the property for search. The parameter name should contain the param- prefix.
Edit Remove
PowerShell
$namingParameter = "param-groupNamePattern" # TODO: modify me
$searchPropertyParameter = "param-propertyName" # TODO: modify me

# Get parameter values
$groupNameEnd = $Context.GetParameterValue($namingParameter)
$searchPropertyName = $Context.GetParameterValue($searchPropertyParameter)

$Context.DirectorySearcher.AppendFilter("(&(objectCategory=group)($searchPropertyName=*$groupNameEnd*))")
$Context.DirectorySearcher.VirtualRoot = $True

try
{
   # Execute search
   $searchResultIterator = $Context.DirectorySearcher.ExecuteSearch()
   $searchResults = $searchResultIterator.FetchAll()   
}
finally
{
   # Release resources
   if ($searchResultIterator) { $searchResultIterator.Dispose() }
}

if ($searchResults.Length -eq 0)
{
    return
}

# Get GUIDs of group members
$memberGuidsBytes = New-Object System.Collections.ArrayList
foreach ($searchResult in $searchResults)
{
   $group = $Context.BindToObjectBySearchResult($searchResult)
   try
   {
       $groupMemberGuidsBytes = $group.GetEx("adm-DirectMembersGuid")
   }
   catch
   {
       continue
   }
   $memberGuidsBytes.AddRange($groupMemberGuidsBytes)
}   

# Generate report
$searcher = $Context.CreateGuidBasedSearcher($memberGuidsBytes)
$Context.Items.Add($searcher)

Comments ( 0 )
No results found.
Leave a comment

Related Scripts