Script Repository


All group memberships of user

July 28, 2017
896

The script emails a report on all group memberships of a user (via direct and indirect membership). The report is sent in HTML format.

To generate such a report on request, you can create a Custom Command that runs the script on User objects.

  • $to - specifies a coma-separated list of recipients of the report. You can use value references to specify recipients. For example, if you specify %adm-InitiatorEmail%, the email will be sent to the email address of the user who runs the script. If you specify %adm-ManagerEmail%, the report will be sent to the email address of the user's manager.
  • $subject - specifies the notification message subject;
  • $reportHeader - specifies the report header;
  • $reportFooter - specifies the report footer.
Edit Remove
PowerShell
$to = "%adm-InitiatorEmail%" # TODO: modify me
$subject = "All AD Groups User '%name%' is a Member Of" # TODO: modify me
$reportHeader = "<h1><b>All AD groups user '%name%' is a Member of (via direct and indirect membership)</b></h1>" # TODO: modify me
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me

# Get GUIDs of groups user is a member of
try
{
    $groupGuidsBytes = $Context.TargetObject.GetEx("adm-MemberOfGuid")
}
catch
{
    $groupGuidsBytes = @()
}

if ($groupGuidsBytes.Length -eq 0)
{
    # Build HTML
    $html = $reportHeader + "<b>The user is not a member of any groups</b>" + $reportFooter
}
else
{
    $table = "<table border=0>"
    foreach ($guidBytes in $groupGuidsBytes)
    {
        $guid = [Guid]$guidBytes
        $group = $Context.BindToObject("Adaxes://<GUID=$guid>")
        
        # Get group name and description
        $name = $group.Get("name")
        try
        {
            $description = $group.Get("description")
        }
        catch
        {
            $description = $NULL
        }
        
        # Add group to report
        $table += "<tr><td>$name</td><td>$description</td></tr>"
    }
    $table += "</table>"
    
    # Build HTML
    $html = $reportHeader + $table + $reportFooter
}

# Send mail
$Context.SendMail($to, $subject, $NULL, $html)


Comments ( 0 )
No results found.
Leave a comment