Splatting the output from Select-Object

Welcome Forums General PowerShell Q&A Splatting the output from Select-Object

Viewing 8 reply threads
  • Author
    Posts
    • #208701
      Participant
      Topics: 9
      Replies: 29
      Points: 191
      Rank: Participant

      Hi again,

      I need to send a number of emails with addresses captured via Get-ADUser. But I would need to grab a number of fields from the cmdlet to personalize the mail.

      Foreach ($UserName in $UserNameArray) {
      Get-ADUser -Server [SERVERNAME] -Identity [USERNAME] -Properties Manager |
      Select-Object GivenName, SurName, UserPrincipalName, Manager
      }

      Basically I’d like to add the outputs from Select-Object to a $UserInfo splat and then call my Send-ReturnMail function with the splat as a parameter.

      The Send-ReturnMail function uses UserPrincipalName as mail address, GivenName and SurName are used in the mail body for personalization and Manager is grabbed to send a cc to the manager of the user.

      Or is there a better way of going about this?

      • This topic was modified 2 months, 3 weeks ago by KLaage. Reason: Expanding on the explanation
    • #208707
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      You don’t need a “select” in this case …

      Foreach ($UserName in $UserNameArray) {
          $ADUser = Get-ADUser -Server [SERVERNAME] -Identity [USERNAME] -Properties Manager 
          $SendMailMessageParams = @{
              from = 'sender'
              to = $ADUser.mail
              subject = "Mail regarding $ADUser.Manager"
              body = 'a lot of very important text'
              smtpserver = mail.server.se
          }
          Send-MailMessage @SendMailMessageParams
      }
    • #208839
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      It still sounds like you need to manipulate the hash before passing it to Send-MailMessage. The $PSBoundParameters is a hash table that you can add and remove and manipulate, so you could remove FrrstName and LastName, build the body and add it to $PSBoundParameters:

      function Send-ReturnMail {
          param(
              [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
              [Alias('UserPrincipalName')]
              [String]$To,
              [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
              [Alias('GivenName')]
              [String]$FirstName,
              [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
              [Alias('SurName')]
              [String]$LastName,
              [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
              [Alias('Manager')]
              [String]$ManagerEmail
          )
      
          $PSBoundParameters
      
          #Send-MailMessage @PSBoundParameters
      }
      
      
      $adUser = @()
      $adUser = [pscustomobject]@{
          GivenName         = 'Jim'
          SurName           = 'Smith'
          UserPrincipalName = 'jim.smith@mycompany.com'
          Manager           = 'sam.manager@mycompany.com'
      }
      
      $adUser | Send-ReturnMail
      
    • #209421
      Participant
      Topics: 9
      Replies: 29
      Points: 191
      Rank: Participant

      Thank you both.

      I’m reevaluating the pipeline a bit, based on input from another post.

      But all this information is invaluable and I appreciate it.

    • #209424
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      Please share the post and your solution so that others may learn.

    • #209613
      Participant
      Topics: 9
      Replies: 29
      Points: 191
      Rank: Participant

      Once I have everything polished up, I’ll post an explanation of how I went about it.

    • #210087
      Participant
      Topics: 9
      Replies: 29
      Points: 191
      Rank: Participant

      Had a lot of help in another thread that tied into this, so there’s code here that’s not directly related to this question, but I thought it was better to present it in context. Basically it went from a splat to using [PSCustomObject].
      Here’s the (non-final) solution… Will still need to test it all properly.

      $reg = '(?[A-Z]{2}\d{4}).* DOMAIN\\(?[a-z]{2}\d{4})'
      
      $ReturnArray = Switch -Regex -File $filePath {
          { $_ -cmatch $reg } {
              $_ -cmatch $reg | Out-Null
              [PSCustomObject]@{
                  ComputerName = $Matches['cname']
                  UserName = $Matches['uname']
              }
          }
      }
      
      $ReturnArray | ForEach-Object {
          $User = Get-ADUser -Server $server -Identity $_.UserName -Properties Manager
          if ((Get-ADUser -Server $server -Identity $_.UserName -Properties Manager).Manager -ne $null) {
            $Manager = Get-ADUser -Server $server -Identity $User.Manager -ErrorAction Stop
          }
          else {
            $Manager = Get-ADUser -Server $server -Identity $_.UserName
          }
          [PSCustomObject]@{
              User = $_.UserName
              GivenName = $User.GivenName
              SurName = $User.Surname
              To = ($User.UserPrincipalName).ToLower()
              Cc = ($Manager.UserPrincipalName).ToLower()
              Computer = $_.ComputerName
          } | Get-MailInfo
      }
      
      function Get-MailInfo {
          [CmdletBinding()]
          param(
              [Parameter(ValueFromPipeline = $true)]
              [Object]$obj
          )
          $mailParams = @{
      	  Credential  = $credential
      	  SmtpServer  = 'smtp.email.address'
      	  Port        = '587'
      	  UseSSL      = $true
      	  Encoding    = 'UTF8'
      	  ReplyTo     = 'helpdesk@email.address'
      	  From        = 'helpdesk <help.desk@email.address>'
      	  BodyAsHtml  = $MailBody
      	  Subject     = $MailSubject
      	  To          = $($obj.To)
      	  Cc		  = $($obj.Cc)
        }
      	
      	$MailSubject = "Regarding your computer: $($obj.Computer)"
      	$mailBody = @"
      Hello $($obj.GivenName) $($obj.SurName),
      Your computer $($obj.Computer) needs work.
      "@
      
      	Send-MailMessage @mailParams -WhatIf
      }
    • #210096
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Did you test this already? The parameter -BodyAsHtml is a switch parameter. The parameter to supply the body of an email is -Body.;-)

    • #210135
      Participant
      Topics: 9
      Replies: 29
      Points: 191
      Rank: Participant

      Went a bit fast throwing together the $mailParams block, so not tested yet, no… Would have caught that at that phase.

      Revised version:

      $mailParams = @{
      	Credential  = $credential
      	SmtpServer  = 'smtp.email.address'
      	Port        = '587'
      	UseSSL      = $true
      	Encoding    = 'UTF8'
      	ReplyTo     = 'helpdesk@email.address'
      	From        = 'helpdesk <help.desk@email.address>'
      	BodyAsHtml  = $true
      	Body        = $MailBody
      	Subject     = $MailSubject
      	To          = $($obj.To)
      	Cc		  = $($obj.Cc)
      }
      • This reply was modified 2 months, 2 weeks ago by KLaage. Reason: Wrong code pasted
      • This reply was modified 2 months, 2 weeks ago by KLaage.
Viewing 8 reply threads
  • You must be logged in to reply to this topic.