Manage PowerShell Parameters with Splatting
1K views
Jun 11, 2024
Organize your parameters with splatting! Learn how to define a hashtable or array for splatting your parameters and even a script example of making parameters dynamic. ? Get the Code Here! ? https://github.com/JeffBrownTech/jeffbrowntech_youtube/tree/main/2024-06-03-PowerShellSplatting ? Read More About This ? https://jeffbrown.tech/powershell-splatting/ ⏰ Chapters ⏰ 00:00 Introduction 00:13 Splatting Syntax and Example 02:01 Use Named Parameters with Splatting 03:28 Using Arrays for Splatting 04:58 Splatting Script Example 06:43 Summary Social Links ? LinkedIn: https://www.linkedin.com/jeffbrowntech ? X: https://www.twitter.com/jeffwbrown ? Threads: https://www.threads.net/@jeffbrowntech
View Video Transcript
0:00
What's up, everyone
0:00
Today, we're going to talk about PowerShell Splatting. This is actually a PowerShell feature that I forget to exist every now and then
0:06
but it is super handy for organizing your commands and making them shorter if you have to work with a lot of parameters
0:12
Let's go ahead and get started. Again, these code examples will be out in my Jeff Brown Tech YouTube repo and GitHub
0:19
and I'll be sure to include a link to it down in the description. First, let's cover Splatine syntax
0:26
It is simply just a hash table. We can see it here on lines 4-3
0:29
You have a dollar sign variable name. We have our hat sign and curly brackets here to denote our hash table
0:38
And then we just have key value pairs. Now the keys are going to be the parameter names that are associated with the command
0:45
you want to use this with. And then the equal sign is the value
0:49
Just like you would use in a parameter, you have the parameter name and the value
0:53
Here we'd have those in our key value pairs. So what does this look like if we want to use it in a command
0:59
Let's create a hash table for running the PowerShell Command new item
1:04
We have dollar sign our hash table, that's our variable name. We have our hash table definition here
1:10
And then name, path, and item type are the parameters that are associated with new item
1:16
And then we have their various values, errors. log, c temp, and our item type is file
1:22
Let me go ahead and load this into our window here. And then what we need to do to use this with our command is right here on line 17
1:33
We're going to run new item. Now to reference your hash table in splatting instead of a dollar sign, you're going to use
1:40
an at sign, and just the same name of the verbal you defined earlier
1:46
So let highlight this and run it And there we go We have created our new file here So again just to recap you creating a hash table name value pairs that match up with parameters and values you want to use for your command
2:01
Now, just because you use a hash table doesn't mean you can't use other parameters when you
2:09
call the command. So right here on line 20, we're still calling our ARG hash table using the at sign
2:16
But let's say I wanted to put a value into the. that file, I can still use the dash value parameter, and then what other value I want to put in
2:26
for that parameter. Let's actually go back and remove that item before I try to run this again
2:33
so let's go errors.log, and then let's create our new item again. See, runs just fine
2:41
and if we were to do a Git content on temp and errors, we can see it has our value in that
2:49
that we used. This is just showing whenever you use splatting with your command
2:54
you can still add other named parameters and the values for those
3:00
And in fact, you can even highlight anything you have within your hash table
3:05
So again, we have our hash table here. We're going to call new item, but you can see I'm duplicating
3:11
the name parameter here. And instead of calling our file errors.log, we're going to call it warning
3:19
I can just call this here and overwrite what is in the hash table and it goes and creates
3:25
the warnings.log file for me. Instead of using a hash table to define your splatted values you want to use with your command
3:34
you can also use a raise. Let's take a look at example of that
3:39
On line 34, we have rename item. We have a name parameter called Path and our value and then another name parameter in new name these parameters also have positional values in them of zero and one so we don actually have to specify them when we call rename item
3:55
On line 37, we could just say rename item, errors.log, and errors dash archive. log
4:03
Those correspond with their positional parameters, path, and new name. And if you think of an array, those also have index values of 0, 1, 2, etc
4:13
however big the array is, this means you can define array and put the values in there that
4:21
you need to match up with your positional parameter. So in line 40 we've defined Argyray here and at index 0 we have CTMP errors
4:31
dot log which is going to match up with our path parameter and then the errors dash archive
4:37
which is at index 1 and matching up with positional parameter 1 for new name
4:44
Go ahead and highlight our array here and get it into our session and then be able to call rename
4:53
item at Array and it's going to rename the item for us
4:58
Let's take a look at an example of how you can use splatting inside of a script
5:03
I have a script here called new Azure Storage account. It has three parameters, the storage account name, a resource group, and the environment
5:11
What we're going to do is have different hash tables created based on which environment we pass into the script
5:17
Starting on line 18, we're going to define our base hash table here called storage count arrays
5:24
We have our account name and our resource group name. Now location and skew name are both null because we are going to set these values based on what we pass in for the environment parameter
5:35
Scrolling down here on line 27 I have a switch statement We looking at the value of environment If the environment is dev we going to append the count name the dev string We going to set the location to West US and our skew for standard LRS
5:52
If we're in our testing environment, we're going to append test. That's actually going to go over to the East US and keep it at standard LRS
6:00
But if we are in the prod environment, we're appending prod. We're putting a different location
6:07
We're updating our skew to a more premium skew. And then we're actually going to add two additional things to our hash table here
6:15
to make sure we're only using HTTPS traffic and setting our minimum TLS version
6:21
So you're not even bound by what you initially define in the hash table
6:25
You can add additional things to it later based on what you need inside of your script
6:30
And then right down here at the end, we just need to call new AZ storage account
6:35
Pass in our at Storage Account ARGs of splatted value here, and it'll go through and create our storage account as needed
6:43
Hopefully this example shows how you can use Splatting to make your commands maybe a little bit cleaner
6:49
You can set different parameter values based on conditions inside of your script
6:55
and sometimes it just makes it easier to see everything that you're passing into a command
6:59
instead of having everything on one line or using line breaks to put all your parameters down below the command, maybe
7:08
Just maybe a little bit cleaner way to showcase this inside of your scripts or functions
7:13
That does it for this video. Thank you for watching, and we'll see you next time
#Computers & Electronics
#Programming