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'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)
0 votes
1 answer

The default pattern format we need should be :First letter of User firstname concatinated to user lastname and pd.sandiego.gov as in jdoe@pd.sandiego.gov

asked Jan 23 by hhsmith (40 points)
0 votes
1 answer

Hi team, I am trying to set a primary smtp address to a user based on input during creation. If someone set a specific email in form, this is stored in customAttributeText3 and ... a specifc email address as primary? Do I need to run a PS command to set it?

asked Oct 13, 2023 by wintec01 (1.1k points)
0 votes
1 answer

My security team is looking to do a security review and would like the vendor to fill out a questionnaire.

asked Aug 25, 2023 by LarrySargent (20 points)
0 votes
1 answer

Hi I've found a small problem in 2021.1 where approval emails for a task are being sent to the onmicrosft.com email address of the approvers not their actual email address. ... , these show up in Mimecast just as the users name, no email address. Thanks Matt

asked Dec 2, 2022 by chappers77 (2.0k points)
3,346 questions
3,047 answers
7,782 comments
544,982 users