Leaver Script – Record AD groups and email them before changing group members

Welcome Forums General PowerShell Q&A Leaver Script – Record AD groups and email them before changing group members

Viewing 5 reply threads
  • Author
    Posts
    • #182178
      Participant
      Topics: 8
      Replies: 9
      Points: 138
      Rank: Participant

      Hi,

      I have a leaver’s script that emails a summary into the support ticket about the leaver that was disabled. I’d like to add a part that emails the AD group membership of that user (so we have it in the ticket) and then removes their AD group membership. I’ve managed to get it working but the email is very hard to read. The script is below.

      Even though I’m using the format-table command at the end of the adgroups variable, it doesn’t carry across into the email.
      The email I get through is like the following, but I’d like it to be formatted better: https://snipboard.io/ai1rL4.jpg

      Is there any way I can achieve this?

      Thanks in advance for your help.

      Simon

    • #182697
      Participant
      Topics: 25
      Replies: 184
      Points: 657
      Helping Hand
      Rank: Major Contributor

      format table really just changes from objects to a view for the console.

      try replacing |ft name with |select-object name

       

    • #182772
      Participant
      Topics: 11
      Replies: 127
      Points: 764
      Helping Hand
      Rank: Major Contributor

      Have you considered using the EnhancedHTML2 module? There’s a bit more work to get the formatting you want but there’s a lot more you can do.

      • #182784
        Participant
        Topics: 6
        Replies: 108
        Points: 304
        Helping Hand
        Rank: Contributor

        And I was thinking about going with some kind of HTML output, but if he is trying to send this to a ticketing system, it probably only supports text as notes. If you can use HTML, Aaron is right. You can do a lot more and make it quite a bit more readable.

    • #182781
      Participant
      Topics: 6
      Replies: 108
      Points: 304
      Helping Hand
      Rank: Contributor

      What David said is probably what’s going on. But the output doesn’t completely match what you’re trying to do, either, and that was messing with me.

      This line is pushing only the “EmailAddresses” attribute for the mailbox for $adacct.UserPrincipalName. It doesn’t output the “Name” and “Alias” attributes. That has to do with the -ExpandProperty parameter and the way it’s treated, I think.

      If you want all 3, you’ll have to put them into separate variables or you’ll have to do some other manipulation before you dump it to an email.

      And I’m still not sure what’s going on with the way it’s displaying $adgroups… It looks like the istinguishedName attributes are showing up in the email, even though it should be the Name attributes. But the line that David posted should give you a list containing the names of the groups that user is a member of. You may have to add “| Out-String” to the end of his line to make it work with your concatenation.

    • #183195
      Participant
      Topics: 8
      Replies: 9
      Points: 138
      Rank: Participant

      Hi all,

      Thanks for your help. It still didn’t work with the changes but I changed the way I was going to do this slightly. Instead of formatting it in the email, I had the AD groups output to a text file that was then attached in the email. That gives me exactly what I need.

      Thanks again,

      Simon

    • #183276
      Participant
      Topics: 11
      Replies: 127
      Points: 764
      Helping Hand
      Rank: Major Contributor

      If you’re wanting to keep email content in the script, a here-string works for me. It will preserve the formatting.

      Assign $emailBody to Body in the splat.

Viewing 5 reply threads
  • The topic ‘Leaver Script – Record AD groups and email them before changing group members’ is closed to new replies.