Splatting in PowerShell
PowerShell "splatting" is a technique that allows you to bundle command-line parameters - typically in a hashtable - so you can pass them as a group to a cmdlet. This practice results in cleaner, more readable, and easier-to-maintain scripts. In the world of Business Central automation and container management, using splatting makes it easier to work with long lists of parameters that Business Central PowerShell modules, like BcContainerHelper, require. It’s a game changer for those who build and manage large script files or even modules.
What is Splatting in PowerShell?
Splatting involves assigning parameters to a hashtable (or an array, when passing positional parameters, not recommended!) and then "spreading" (or splatting) that hashtable to a command using the @ syntax. Under the hood, PowerShell takes each key/value pair in your hashtable and passes it as if you had explicitly written out each parameter. This technique leads to code that’s much more legible - especially when commands accept many options. Splatting helps:
- Enhance Readability: Long parameter lists can be placed in a neatly formatted block.
- Simplify Maintenance: Adjust parameter values in one central location, making your scripts easier to modify or reuse later.
- Avoid Errors: With splatting, you’re less likely to miss or mistype a parameter name in the command line since everything is contained within a hashtable.
Real-World Example
These benefits are clear when you consider how many configuration options are required to set up a Business Central Docker container correctly.
$NewBcContainerSplat = @{
accept_eula = $true # Switch Parameter
imageName = 'my'
licenseFile = 'c:\my.bclicense'
# ...
}
$ArtifactUrl = Get-BcArtifactUrl -country de
New-BcContainer @NewBcContainerSplat -containerName 'sandbox_de' -artifactUrl $ArtifactUrl
As you can see, you can use splatting together with normal parameter specifications, for example to distinguish parameters that you never change from those that you change regularly, such as the container name or the artifact token.
Don't Repeat Yourself
DRY is one of the core concepts in professional software development. It describes that redundancies should be avoided in order to create a single point of truth. This is achieved by trying (as far as reasonable) to avoid duplicated information or logic. Splatting can also help us here.
It often happens that several commands are executed with the same subset of parameters. These can be combined in a splat and used repeatedly.
$ServerAndTenantSplat = @{
ServerInstance = 'navserver'
tenant = 'staging'
}
# Get Tenant ans Apps
$CurrState = Get-NavTenant @ServerAndTenantSplat
$Apps = Get-NavAppInfo @ServerAndTenantSplat -TenantSpecificProperties
# Start Upgrade and Wait till finished
Start-NAVDataUpgrade @ServerAndTenantSplat
do {
Sleep 3000
}
while (Get-NAVDataUpgrade @ServerAndTenantSplat).State -eq 'InProgress')
Downsides
If splatting is used, it must be clear that every key in the hashtable can also be accepted by the target function. for example, a splat variable must not have a key "path" if the function does not also have a parameter with the same name.
Unfortunately, most IDEs (e.g.: Visual Studio Code) do not support splats. for example, it is not recognized if a parameter has already been set via a splat. It is still suggested again via IntelliSense.
Unfortunately it is not possible to overwrite a parameter which is contained in the splat during the actual cmdlet call.
Conclusion
Splatting can transform your PowerShell scripting experience, particularly when working with feature-rich modules like BcContainerHelper in Business Central. With splatting, you gain cleaner code, enhanced maintainability, and a streamlined way to manage lengthy parameter lists. Splatting helps keep your code both beautiful and functional.
For those managing Business Central environments, embracing splatting in your scripts means quicker adjustments and fewer headaches during those late-night troubleshooting sessions. Dive into your Business Central automation projects and witness firsthand how a small change in your script strategy can make a massive difference.