T-SQL Tuesday
This month’s T-SQL Tuesday is hosted by Lisa Griffin Bohm. (b|t). Lisa asks “This month, I’d like those of you who have presented, or written a presentation, to share something technical THAT DID NOT RELATE to the topic of the presentation, that you’ve learned in writing or giving the presentation.” I’m going to write about how I came across PowerShell Splatting and how it made me better at PowerShell, despite presenting on a cloud topic.
Powershell splatting
At DataScotland 2019, I did a presentation on AWS RDS for SQL Server. The technical content was about how RDS works, what you can do with it, and how to provision it etc. As part of my demos, I decided to use AWS PowerShell commands. When I had made this presentation at previous events, I had used AWS CLI so I had to update my code examples. I’m ok with PowerShell, but I’m not an expert. I just wanted to show that were were multiple ways to interface with AWS.
My code was full of backticks. You could say I was daft about backticks. I loved them and thought they made my PowerShell code readable because they stopped the lines from running off the monitor. Someone asked me why I don’t use PowerShell splatting? “Whatting” I asked? I had never heard of splatting.
At the break, I spoke to a couple of people who were more experienced in PowerShell than me. They advised that PowerShell splatting was the way to go for large commands because they make the code more readable. More readable was definitely something I was interested in so I decided to go away and learn about splatting, and update my code for future events.
So what is PowerShell Splatting?
Rather than passing a long list of parameters into commands, you can create a variable in advance to hold these values. The variable is an array or a hash table and includes as many parameters as you need. If you need to pass in parameter names and values, use a hash table. If you just need to pass in a list of parameter values, you should use an array. Then, when you run the command, you simply pass in the hash table parameter instead of all the individual parameters.
Example command before splatting
New-RDSDBInstance -dbinstanceidentifier "datascotland-posh" -region "eu-central-1" -VpcSecurityGroupId "sg-00a1234g567c3d4ab" ` -allocatedstorage 20 -dbinstanceclass "db.t2.micro" -engine "sqlserver-ex" ` -masterusername "rds_name" -masteruserpassword "secure_pw_here" -availabilityzone "eu-central-1a" ` -port 50000 -engineversion "14.00.3049.1.v1"
Example command after splatting
$HashArguments = @{ dbinstanceidentifier= "datascotland-posh" region = "eu-central-1" VpcSecurityGroupId = "sg-00a1234g567c3d4ab" allocatedstorage = 20 dbinstanceclass = "db.t2.micro" engine = "sqlserver-ex" masterusername = "rds_name" masteruserpassword = "secure_pw_here" availabilityzone = "eu-central-1a" port = 50000 engineversion = "14.00.3049.1.v1" } New-RDSDBInstance @HashArguments
At a glance
As you can see, the 2nd example which uses splatting is easier to read and you can pick out the value of each parameter at a quick glance. It was worth learning to make my code more readable and improve my overall PowerShell knowledge.
“Writing this post has just made me realise that I should update my RDS course ↙ as the examples in it don’t use splatting. 🤦♂️”