0 votes

Hi,

I have a piece of PS code being called by Adaxes and I'm trying to email each AD group member after they are removed from an AD group.

Currently I am unable to find a simple way of retrieving the email address. It looks (as far as I can tell) that the email (mail) attribute is not being stored when the user objects are retrieved - my code snipped is as below where the $emailto variable is always coming back blank (but I can change to .Name etc and it does pull back the correct data for that field).

Is there a simple way to include the mail attribute in the search? Note that I have tried $member.Get("mail") also.

Thanks

Import-Module Adaxes
$groupMembers = Get-AdmGroupMember -Identity "%distinguishedName%" -Server "lan.net" -AdaxesService localhost
ForEach ($member in $groupMembers)
{
    Remove-AdmGroupMember -Identity "%distinguishedName%" -Members $member -Server "lan.net" -AdaxesService localhost -Confirm:$False
    $Context.LogMessage("User removed: " + " $member", "Information")

    $emailto = $member.mail
...
by (1.6k points)

1 Answer

0 votes
by (216k points)
selected by
Best answer

Hello,

The following code will do the job:

foreach ($memberGuidInByte in $Context.TargetObject.GetEx("adm-DirectMembersGuid"))
{
    $memberGuid = New-Object "System.Guid" (,$memberGuidInByte)
    $memberGuid = $MemberGuid.ToString("B")
    $memberPath = "Adaxes://<GUID=$memberGuid>"

    # Remove the member
    $Context.TargetObject.Remove($memberPath)

    # Bind to the member
    $member = $Context.BindToObject($memberPath)
    $Context.LogMessage("User removed: " + $member.Get("name"), "Information")

    # Get e-mail address
    try
    {
        $mailTo = $member.Get("mail")
    }
    catch
    {
        continue # The member doesn't have an e-mail address
    }
    ...
0

Hi. Thanks very much for this.

I am getting an error when trying to run - almost certainly my fault as I'll be honest and say I'm not 100% sure what I'm doing when dealing with/converting GUID's etc.

I have replicated the core error with a cutdown script as shown in the below screenshot - it looks as if the GUID coming back is being sliced up as there is ony one member in my test group but it is looping through multiple portions of the GUID?


Many thanks

0

Hello,

Sorry, there was a small error in the script. We've modified the script in the post above, just recopy it and try again.

Also, in your version of the script, you have a statement that loads the Adaxes ADSI DLL. Remove it. The DLL is used everywhere in Adaxes to perform any operations in Active Directory, so it is already loaded into all PowerShell sessions created by Business Rules, Custom Commands and Scheduled Tasks.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

0

Perfect - thanks very much.

To confirm to anyone reading this because they have a similar issue the change was adding:- $Context.TargetObject.GetEx("adm-DirectMembersGuid")).

Many Thanks

Related questions

0 votes
1 answer

Hello, I want to include in a sent email notification after removing a member from a group the active directory user property "company" and "co" of a user. How can I achieve this? I can not select a property %adm-membercompany% nor %adm-memberco"

asked Jun 7 by fabian.p (380 points)
0 votes
1 answer

feature request: Allow scheduled reports to have any email address as a recipient.

asked Nov 24 by PaulPCGuy7 (20 points)
0 votes
1 answer

Hello, Was wondering if there is a script for a business rule or a property pattern that can be used for this request. I am wondering how can I do a check ... $Context.LogMessage("The email address '$emailAddress' is available.", "Information") return $false }

asked Nov 13 by KoleArmstrong (60 points)
0 votes
1 answer

Hi all, I've created a 'View User' command via the Web Interface Configurator. I don't want users to be able to make any changes, so I've created a Custom ... there any way to remove this, short of removing the entire Exchange section itself? Thanks, David

asked Sep 5 by dshortall (80 points)
0 votes
1 answer

Hello, I'm wondering if it's possible to export a list of all users in AD along with their email addresses to an Excel spreadsheet and then schedule that export to append ... address that wasn't previously used. Please let me know if this is possible. Thanks!

asked Apr 11 by sjjb2024 (60 points)
3,589 questions
3,278 answers
8,303 comments
548,122 users