Splatting Send-MailMessage with different Subject

Hi all,

Been working on my splatting skills (Very limited at the moment)

If been working on this little project

[pre]

$mailmessage = @{
To = ‘My@mail.nl’
from =‘Your@mail.nl’
Smtpserver=‘My mail server’
subject = ‘E-Mail niet uniek’,‘EMail is Uniek’
UseSsl = $true
BodyAsHtml = $true
}

Send-MailMessage @mailmessage -Subject 1

Send-MailMessage @mailmessage -Subject 2

[/pre]

 

Is it possible like this so i can choose what subject i want to send,

Or is the only possibillity to use 2 splats :slight_smile:

Theproperty inside splat hashtable cannot be passed via splatting, you can exclude that in splat hashtable and pass it separately.

$MailMessage = @{
  To         = 'My@mail.nl'
  From       ='Your@mail.nl'
  SmtpServer ='My mail server'  
  UseSsl     = $true
  BodyAsHtml = $true
}

Send-MailMessage @mailmessage -Subject 'E-Mail niet uniek'

Thanks for the reply kvprasoon,

Was hoping there whas another way.

Wanted to keep all subjects in one place so i dont have to wonder true my code to find them.

Ill use $subject1 and $subject2 then for now

Hi Bart,

I believe you can achieve this by creating a function that excepts pipeline input for the subject.

function Test-SubjectSplat {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline)]
        [string[]]$Subject
    )

    Process {
        foreach ($Item in $Subject) {
            $MailMessage = @{
                To         = 'My@mail.nl'
                From       = 'Your@mail.nl'
                SmtpServer = 'My mail server'
                Subject    = $Item 
                UseSsl     = $true
                BodyAsHtml = $true
            }
            Send-MailMessage @MailMessage
        }
    }
}

'E-Mail niet uniek','EMail is Uniek' |Test-SubjectSplat

Another alternative is just to modify the splat table between command calls. Using your original code:

$mailmessage = @{
    To = 'My@mail.nl'
    From ='Your@mail.nl'
    SmtpServer='My mail server'
    Subject = 'E-Mail niet uniek'
    UseSsl = $true
    BodyAsHtml = $true
}

Send-MailMessage @mailmessage

$mailmessage['Subject'] = 'E-Mail is Uniek'
Send-MailMessage @mailmessage

To iterate on Joel’s suggestion, you could wrap it in a function and specify one of two subjects, then modify the parameters accordingly. If neither subject is supplied, the subject defaults to blank.

[pre]
Function Send-CustomSubject {
[cmdletbinding(DefaultParameterSetName=‘A’)]
param(
[Parameter(ParameterSetName = ‘A’)]
[switch] $SubjectOne,

    [Parameter(ParameterSetName = 'B')]
    [switch] $SubjectTwo 
)
$mailmessage = @{
    To = 'My@mail.nl'
    From ='Your@mail.nl'
    SmtpServer='My mail server'
    Subject = ' '
    UseSsl = $true
    BodyAsHtml = $true
}
If ($SubjectOne) {
    $mailmessage['Subject'] = 'E-Mail niet uniek'
}
If ($SubjectTwo) {
    $mailmessage['Subject'] = 'EMail is Uniek'
}

Send-MailMessage @mailmessage

}
[/pre]

We can see the hashtable change accordingly:

[pre]
PS /Users/admin> Send-CustomSubject

Name Value


BodyAsHtml True
SmtpServer My mail server
UseSsl True
Subject
From Your@mail.nl
To My@mail.nl

PS /Users/admin> Send-CustomSubject -SubjectOne

Name Value


BodyAsHtml True
SmtpServer My mail server
UseSsl True
Subject E-Mail niet uniek
From Your@mail.nl
To My@mail.nl

PS /Users/admin> Send-CustomSubject -SubjectTwo

Name Value


BodyAsHtml True
SmtpServer My mail server
UseSsl True
Subject EMail is Uniek
From Your@mail.nl
To My@mail.nl

[/pre]

Thanks for all the suggestions,

I’ve learned some splatting skills :slight_smile: