Scripting Tips and Tricks: Script Development with Splatting
Advice can be given, ignored or taken. Over the next few weeks I'll write about some scripting practices I find useful and helpful. You can decide if these tips are for you...
When writing a longer script I often have to work on it as and when I can. These scripts tend to have lots of parameters and I like to perform testing again and again... and again.
Given the nature of my work, it's possible that a few days may pass before I return to a script and it's also possible that my laptop has been rebooted... so, to save set-up time, I'll often save parameters and values to a CSV file so they can be easily imported and splatted into a work-in-progress script.
What's splatting? A technique from the Ruby scripting language that allows parameters to be defined up front and then passed as one entity.
Here's what I do to get myself back up and running quickly after each reboot...
- Create a CSV file of parameters and values (you need only do this once)
- Dot source a script that adds the parameters and values to an OrderedDictionary object called $Properties
Load_Ordered.ps1:
$Properties = [Ordered]@{}
Get-Content .\Properties.csv | ForEach-Object {$Properties.Add($($_ -split ",")[0],$($_ -split ",")[1])}
- Splat the properties into the script I'm working on
Now, this doesn't look like much... believe me, it saves time and bother redefining parameters and values when I return to a script. You could even add the code to your profile...
Take it or leave it... you chose.
Comments
- Anonymous
December 18, 2015
Add a vendor-specific class indentifier for Cisco AP c1600 Series devices with PowerShell. Also add an option definition for the new class.