Hello,
We have updated the script to meet your needs, find it below. To run the script, you can use a Custom Command, Scheduled Task, etc. configured for Group object type. In the script:
- $csvFilePath - specifies a UNC path to the CSV file that will be created by the script;
- $removeCSVFile - specifies whether to remove the CSV file after it has been sent;
- $sortColumns - specifies the columns to sort by;
- $sortDirection - specifies the sort direction;
- $to - specifies a comma separated list of recipients of the report;
- $subject - specifies the email message subject;
- $message - specifies the email notification message;
- $from - specifies the notification sender;
- $smtpServer - specifies the SMTP server to use when sending a notification.
# CSV file settings
$csvFilePath = "C:\reports\report.csv" # TODO: modify me
$removeCSVFile = $True # TODO: modify me
# E-mail settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Group members" # TODO: modify me
$message = "Group members" # TODO: modify me
$from = "noreply@domain.com" # TODO: modify me
$smtpServer = "mail.domain.com" # TODO: modify me
function GetPropertyValue ($object, $propertyName)
{
try
{
$value = $object.Get($propertyName)
}
catch
{
$value = $NULL
}
return $value
}
$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 = GetPropertyValue $member "cn"
$memberEmail = GetPropertyValue $member "mail"
$reportEntry = New-Object PSObject
$reportEntry | Add-Member -Name Name -Value $memberName -MemberType NoteProperty
$reportEntry | Add-Member -Name Email -Value $memberEmail -MemberType NoteProperty
$report += $reportEntry
}
# Export report to CSV
$report | Sort-Object "Name" | Export-Csv -Path $csvFilePath -NoTypeInformation
# Send mail
Send-MailMessage -To $to -from $from -SmtpServer $smtpServer -Subject $subject -Body $message -Attachments $csvFilePath
if ($removeCSVFile)
{
# Remove temporary file
Remove-Item $csvFilePath -Force
}