Splatting Parameters

This is a quick post on using hash tables to splat parameters to help organize and tidy up your powershell scripts.

I’m going to assume that everyone is familiar with the concept and simply present a quick sample script. It’s intentionally very basic (no parameter validation or error checking). The premise is that you have a csv file from HR with a list of user names and the number of vacation days they have remaining. You need to send them an email notification and CC their manager.

First, this script without any splatting:


Param (
$ImportFile     =  './user_vacation_list.csv',
$EmailFrom      =  'HR@mycompany.com',
$EmailSubject   =  'Unused vacation reminder',
$SMTPServer     =  'mail.mycompany.com'
)

Foreach ($User in inport-csv $ImportFile)
 {
   $ADUser = Get-ADUser $User.Name -Propertes Mail,Manager
   $Manager = Get-ADUser $User.Manager -Properties Mail

   $EmailTo = $ADUser.Mail
   $EmailCC = $Manager.Mail
   $EmailBody = "You have $($User.VacationDays) unused vacation days."

Send-MailMessage -From $EmailFrom -To $EmailTo -Cc $EmailCC -Subject $EmailSubject -SmtpServer $SMTPServer -Body $EmailBody

}

Now I’m going to do the same thing, but using hash tables to pass my parameters to send-mailmessage:

Param (
$ImportFile     =  './user_vacation_list.csv',
$EmailFrom      =  'HR@mycompany.com',
$EmailSubject   =  'Unused vacation reminder',
$SMTPServer     =  'mail.mycompany.com'
)

$AllEmailParams =
        @{
          From       = $EmailFrom
          Subject    = $EmailSubject
          SMTPServer = $SMTPServer
          }

Foreach ($User in inport-csv $ImportFile)
 {
   $ADUser = Get-ADUser $User.Name -Propertes Mail,Manager
   $Manager = Get-ADUser $User.Manager -Properties Mail

   $ThisEmailParams = 
        @{
          To   = $ADUser.Mail
          Cc   = $Manager.Mail
          Body = "You have $($User.VacationDays) unused vacation days."
         }

 Send-MailMessage @AllEmailParams @ThisEmailParams
 }

The “fixed” parameters that are being passed as script parameters can be in one hash table, close to the param block where it’s easy to associate the script parameters being passed with the send-mail message parameters they will map to.

Another hash table inside the loop is used to hold and set the parameters that will unique to each email, and then both are splatted to the command when it’s time to send the email. I think this produces much cleaner and more intuitive code.

Advertisements

4 responses to “Splatting Parameters

  1. I didn’t know you could double splat like that. I like the separation this provides. Good tip.

  2. You stole my thunder sir šŸ™‚ I do love splatting and agree that it makes the code look nicer and read easier.

  3. Pingback: Us a script cache to speed up repetitive directory lookups | The Powershell Workbench

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s