Leveraging Splatting in PowerShell Scripts

While presenting a PowerShell demo at VMworld, one gentleman asked me about splatting. Essentially – what is it, how does it work, and an example showing it in use. Sure!

First, let’s take a step back to recognize that most functions and cmdlets in PowerShell have parameter requirements. They need inputs in order to perform work. You can easily see this by using the Get-Help cmdlet on a function or cmdlet. I’ll demonstrate using one of the functions written for the Rubrik module.

Get-Help Protect-RubrikTag -Detailed

The output will contain a long list of things. The one we’re interested in is Parameters.

PARAMETERS
 -Tag <String> 
 -Category <String> 
 -SLA <String> 
 -vCenter <String> 
 -Server <String>

Some of them are optional and will have a pair of brackets around them. Others are required. Using the Get-Help cmdlet is nice because it shows all parameters in full. Normally, we’d have to answer all of the parameter requirements by putting them into the cmdlet or piping answers into the cmdlet.

Example:

Protect-RubrikTag -Tag 'Gold' -Category 'Rubrik' -vCenter 'vcenter.example'

Sometimes, this can get really, really long. It can also be difficult to track the answers to parameters when done this way, especially if there are a lot of them and the assignments are being done using other variables. This is where splatting can help.

The Science of Splatting

Splatting is a weird word. For some reason, it reminds me of spaghetti being thrown against a wall. Splat!

splat-image

The idea behind splatting is to provide answers to a list of parameters by using a hashtable. In simpler terms, we take the name of each parameter, assign a value to it, and store that into a variable. This variable is then passed along to the function or cmdlet that needs inputs. By parsing through the “splat” variable, the command is able to answer all of its parameter value requirements.

Taking the example above, let’s build a splat variable cleverly just named $splat. We’ll store a series of key-value pairs into the hashtable that answer the parameter requirements. In this case, the Tag, Category, and vCenter FQDN.

$splat = @{
 Tag = 'Gold'
 Category = 'Rubrik'
 vCenter = 'vcenter.example'
}

Now that the variable has been created, it can be given over to the function. Note that in this case, the variable is represented using the @ symbol instead of the typical variable $ symbol. I think this trips a lot of people up, so watch out!

Protect-RubrikTag @splat

There are no inputs required because the splat answers all questions put forth by the function. The function will now go out and find any VM tagged with Gold so that it can associate the Gold SLA Domain to them. I’ve found that it’s still perfectly acceptable to use a -Confirm or -WhatIf after the splat if so desired.

For most commands, this will be overkill. I think it’s a clean way to code in the answers for a number of parameters, though, and you may choose to use this method will working with long strings of information that are being sent to a command. Note that you can also use all of the normal rules for a hashtable, such as input validation and other variables, when constructing the splat.