split array

Welcome Forums General PowerShell Q&A split array

Viewing 3 reply threads
  • Author
    Posts
    • #236110
      Participant
      Topics: 34
      Replies: 82
      Points: 466
      Rank: Contributor

      Hi,
      I use this

      $user= “username”
      $groups =Get-ADPrincipalGroupMembership $user
      $group = $groups.name
      
      Send-MailMessage -To “[email protected]” -From “[email protected]” -Subject “List of groups $user is member off” -Body “ $group Best regards” -Credential $emailcred -SmtpServer “outbound.mydomain.com” -Port 25
      
      

      this will give me the clean name without the “CN=” ect

      now I want to send this by mail to a common mailbox but I do get it printed out like groupA groupB groupC

      by preference I would like to print this

      groupA

      groupB

      groupC

      how do I achieve this?

    • #236170
      Participant
      Topics: 12
      Replies: 1643
      Points: 2,665
      Helping Hand
      Rank: Community Hero

      There is an eBook in Free Resources about HTML reporting you should take a look at, but it’s simpler to use HTML for simple formatting in message bodies:

      $user= “username”
      $groups = Get-ADPrincipalGroupMembership $user |
                Select @{Name='Group Memberships';Expression={$_.Name}}
      
      $htmlBody = $groups | 
                  ConvertTo-Html -PostContent "<br/Regards<br/><br/>IT Folks" -Property 'Group Memberships'
      
      $params = @{
          To         = “[email protected]” 
          From       = “[email protected]” 
          Subject    = “Group Membership(s): $user” 
          Body       = $htmlBody
          Credential = $emailcred 
          SmtpServer = “outbound.mydomain.com”
          BodyAsHtml = $true
          Port       = 25
      }
      
      Send-MailMessage @params
      
    • #236182
      Participant
      Topics: 3
      Replies: 421
      Points: 1,479
      Helping Hand
      Rank: Community Hero

      Dang, Rob is fast! Here’s another way you can accomplish your goal. We both cleaned up the code with splatting, it’s highly recommended for many reasons.

      $user = “username”
      $groups = Get-ADPrincipalGroupMembership
      
      $msgparams = @{
          To         =  “[email protected]”
          From       =  “[email protected]”
          Subject    =  “List of groups $user is member off”
          Body       =  $groups + "Best regards” -join "nn"  # Forum messing this up, supposed to be the grave accent (shift ~) in front of each n
          Credential =  $emailcred
          SmtpServer =  “outbound.mydomain.com”
          Port       =  25
      }
      
      Send-MailMessage @msgparams
      

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7

    • #236206
      Participant
      Topics: 34
      Replies: 82
      Points: 466
      Rank: Contributor

      thanks for your answers

       

      Paul

Viewing 3 reply threads
  • You must be logged in to reply to this topic.