Splatting the output from Select-Object

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?

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
}

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

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.

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

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

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
}

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

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)
}