0 votes

Hello, Similar to exporting the members of a group to a csv file: https://www.adaxes.com/script-repository/export-group-members-to-csv-file-s184.htm

I am looking to export the contents of a groups' memberof tab to a CSV file. How might I modify the above referenced script to export a list of groups that a security group belongs to? I have a business rule generating the above export and emailing use upon group deletion and would like to include the memberof csv report in the email as well.

Thanks in advance!

by (20 points)
edited by

1 Answer

0 votes
by (289k points)

Hello Jonny,

You can use a built-in report, Membership in groups. By default, the report is located in container Reports\All Reports\Groups\Membership. image.png

0

Thanks for your reply. I just edited my question to be more clear.

I am currently generating the group membership report via a business rule with a powershell action before an AD group is deleted and then sending that csv as an attachment in an email via the powershell script. I'd like to include the memberof report as an attachment in the email as well so I am looking for a powershell script to generate that report.

0

Hello Jonny,

Thank you for clarifying. For us to help you with the script, please, provide an example of the CSV file you need. Also, please, provide us with the current script you have sending emails. You can post the details here or send to us at support@adaxes.com.

0

Sure thing!

Example CSV I am looking for: image.png

Below is my current script (Some data has been changed to example data):

# Group Membership Report Begin
$csvFilePath = "C:\exampleFolder\%name%_MembershipReport.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
{

}

# 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
# Group Membership Report End

# Group MemberOf Report Begin
# Group MemberOf Report End

# HTML Email Template
$nowdate = [System.DateTime]::Now.ToString("dddd, MMMM dd yyyy")
$nowtime = Get-Date -Format t
$EmailBody = @"
<div style="width: 50%%; display: block; margin-left: auto; margin-right: auto">
<table style="width: 100%%; border-collapse: collapse; border: 1px solid #202945">
 <tr>
    <td colspan="2" bgcolor="#202945" style="color: #FFFFFF; font-size: large; text-align: center; height: 100px;">
        Security Group Deleted: %cn% <br/> $nowdate at $nowtime
    </td>
 </tr>
 <tr style="border-bottom-style: solid; border-bottom-width: 1px; padding-bottom: 1px">
    <td style="width:120px; height: 50px; padding: 10px">  <b>Initiator:</b></td>
    <td style="text-align: left; height: 35px">
    %adm-InitiatorFullName% (%adm-InitiatorUserName%)</td>
 </tr>
  <tr style="border-bottom-style: solid; border-bottom-width: 1px; padding-bottom: 1px">
    <td style="width:120px; height: 50px; padding: 10px">  <b>Full Operation:</b></td>
    <td style="text-align: left; height: 50px">
    %adm-OperationDescription%</td>
 </tr>
  <tr style="height: 50px">
  <td style="width: 120px; height: 50px; padding: 10px">  <b>Members:</b></td>
  <td style="text-align: left; height: 50px">
  Please see the attached membership report. If the security group contains no members then an empty CSV file will be included.</td>
 </tr>
  </tr>
  <tr style="height: 50px">
  <td style="width: 120px; height: 50px; padding: 10px">  <b>Member Of:</b></td>
  <td style="text-align: left; height: 50px">
  Please see the attached MemberOf report. If the security group is not a member of any groups an empty CSV file will be included.</td>
 </tr>
</table>
</div>
"@

# Send Email
$emails = "exampleEmail@domain.com"
Start-Sleep -s 15
Send-MailMessage -From "example@domain.com" -To $emails -Subject 'Adaxes: Security Group Deleted - %cn%' -Body $EmailBody -BodyAsHtml -Attachments $csvFilePath -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'exampleSmtpServer.domain.com'
Start-Sleep -s 5
Remove-Item -path $csvFilePath
0

Hello Jonny,

We updated the script accordingly. You can check it below.

# Group Membership Report Begin
$csvFilePath = "C:\exampleFolder\%name%_MembershipReport.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-MemberOfGuid")
}
catch
{

}

# 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 ($member.IsMember($Context.TargetObject.AdsPath))
    {
        $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
# Group Membership Report End

# Group MemberOf Report Begin
# Group MemberOf Report End

# HTML Email Template
$nowdate = [System.DateTime]::Now.ToString("dddd, MMMM dd yyyy")
$nowtime = Get-Date -Format t
$EmailBody = @"
<div style="width: 50%%; display: block; margin-left: auto; margin-right: auto">
<table style="width: 100%%; border-collapse: collapse; border: 1px solid #202945">
 <tr>
    <td colspan="2" bgcolor="#202945" style="color: #FFFFFF; font-size: large; text-align: center; height: 100px;">
        Security Group Deleted: %cn% <br/> $nowdate at $nowtime
    </td>
 </tr>
 <tr style="border-bottom-style: solid; border-bottom-width: 1px; padding-bottom: 1px">
    <td style="width:120px; height: 50px; padding: 10px">  <b>Initiator:</b></td>
    <td style="text-align: left; height: 35px">
    %adm-InitiatorFullName% (%adm-InitiatorUserName%)</td>
 </tr>
  <tr style="border-bottom-style: solid; border-bottom-width: 1px; padding-bottom: 1px">
    <td style="width:120px; height: 50px; padding: 10px">  <b>Full Operation:</b></td>
    <td style="text-align: left; height: 50px">
    %adm-OperationDescription%</td>
 </tr>
  <tr style="height: 50px">
  <td style="width: 120px; height: 50px; padding: 10px">  <b>Members:</b></td>
  <td style="text-align: left; height: 50px">
  Please see the attached membership report. If the security group contains no members then an empty CSV file will be included.</td>
 </tr>
  </tr>
  <tr style="height: 50px">
  <td style="width: 120px; height: 50px; padding: 10px">  <b>Member Of:</b></td>
  <td style="text-align: left; height: 50px">
  Please see the attached MemberOf report. If the security group is not a member of any groups an empty CSV file will be included.</td>
 </tr>
</table>
</div>
"@

# Send Email
$emails = "exampleEmail@domain.com"
Start-Sleep -s 15
Send-MailMessage -From "example@domain.com" -To $emails -Subject 'Adaxes: Security Group Deleted - %cn%' -Body $EmailBody -BodyAsHtml -Attachments $csvFilePath -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'exampleSmtpServer.domain.com'
Start-Sleep -s 5
Remove-Item -path $csvFilePath
0

Hello,

This worked great. Thank you!

Related questions

0 votes
0 answers

I am trying to find a way to create Groups based off an OU and a list of options (check boxes) within the portal For example: Select the Target OU to add groups ... 3 - Remote Administrators Option 3 - Remote Developers Option 4 - Readers Option 4 - Writers

asked Sep 11, 2020 by dknapp (100 points)
0 votes
1 answer

I need to send an e-mail to the owner ("managed by") for each group. The e-mail should contain a list of group members. What is the best way to do that?

asked May 9 by akindy (20 points)
0 votes
1 answer

So I need to export a list of all user's Line URI's to a CSV file. Running Adaxes 2021 Version 3.14.18804.0 (64 bit) and Teams Powershell 4.1.0 ... a Microsoft 365 account } finally { # Close the connection and release resources Disconnect-MicrosoftTeams }

asked Aug 4, 2022 by TheLexicon (200 points)
0 votes
1 answer

Hi, I would like to add a CSV file during my group creation form and add users from CSV to new created group. However the CSV file is converted into Binary file and I am ... help is really appreciated. Thanks! PS: What I tried so far and error message I got

asked Jul 3, 2023 by wintec01 (1.5k points)
0 votes
1 answer

Have a csv file of users that I need to import into Adaxes. I had initially found an article for this, but upon going today, it gave me an error (looks like it was deleted). Thank you

asked Nov 19, 2022 by wangl (20 points)
3,548 questions
3,238 answers
8,232 comments
547,813 users