How To Create PowerShell Dynamic Parameters!
1K views
Aug 21, 2024
What are PowerShell dynamic parameters and when can you use them? Find out everything you need to know in this video! ? Get the Code Here! ? https://github.com/JeffBrownTech/jeffbrowntech_youtube/blob/main/2024-08-14-PowerShellDynamicParameters/PowerShellDynamicParameters.ps1 ? Read More About This ? Tips and Tricks to Using PowerShell Dynamic Parameters https://jeffbrown.tech/tips-and-tricks-to-using-powershell-dynamic-parameters/ ? Watch: ? https://youtu.be/RdnQbA3U7HQ?si=vEFT6rl7xLVPFSYa ⏰ Chapters ⏰ 00:00 Introduction 00:27 Define Dynamic Parameters 03:20 Adding Begin/Process/End Blocks are Required 04:21 Putting It All Together 06:21 Summary Social Links
View Video Transcript
0:00
What's up everyone today? We're going to talk about PowerShell Dynamic Parameters
0:04
I feel like I've been making a lot of videos about PowerShell parameters, but I guess they have a lot of functionality and cool tricks that you can do with them
0:11
In this video, we'll learn about dynamic parameters and when you may want to use them in your scripts or functions
0:17
As always, this code is available out in my GitHub, and I'll include a link to it down in the video description below
0:23
My name is Jeff. You're watching Jeff Brown Tech, and let's go ahead and get started
0:27
First, let's talk about exactly what are dynamic parameters. Dynamic parameters are parameters that are available when certain conditions are met
0:34
We're going to go through an example of creating a channel inside of a Microsoft Teams team
0:40
That's an odd way to say that. And you don't need to know anything about Microsoft Teams to understand this example
0:45
I'll go through everything you need to know. First, let's get started with our basic function definition here called new channel
0:52
And we have two parameters, channel name and channel type. They are both mandatory
0:55
and on channel type we have a validate set. It can be of type standard or private
1:02
Now if you create a channel type that is private, you also have to specify a channel owner
1:07
This is only required when the channel type is private. In this case, if somebody is running this function
1:14
and they specify channel type equals private, then I want them to also specify a private channel owner
1:21
Let's see how we can do that with dynamic parameters. First, let's take a look at how to define
1:25
dynamic parameters. We have an updated function here. We have our param section that we already went over
1:31
And right below it, we now use the keyword dynamic param and curly brackets to indicate a script block
1:37
Inside of this script block we start writing out the logic that we want to use for our dynamic parameter In this case it saying if channel type equals private then we going to define our dynamic parameter there
1:51
Notice you can use other parameters that you've already defined inside your dynamic parameter definition here
1:56
In this case, we're using channel type. Let's go through the rest of the logic to define our parameter
2:02
Start on line 54. Again, we're saying if channel type equals private. We're going to create a new object here of parameter attribute
2:10
We're defining this parameter to be mandatory, just like we do in our other parameters
2:14
up here where we say it's mandatory on line 30 and 33
2:19
And then if we come down, we have a couple other things using the dot-net framework
2:23
We're going to create a collection type of system. Dot attribute and add our parameter attributes to it
2:28
Then we're going to create a runtime defined parameter object and give the parameter
2:33
In this case, it's going to be private channel owner. We're going to specify the parameter type, which is going to be string
2:40
Then we're going to add our attribute collection, the Param Attribute Collection variable here that
2:45
we just create a specifying that the parameter is mandatory. And then finally we're going to create a runtime defined parameter dictionary
2:55
And we're going to store our runtime to find parameter object in it and then return that
2:59
parameter dictionary. I'll fully admit there are a lot of things in here that I don't understand in creating things with dot net using these different methods or classes and things like that in defining our Parachshel parameters here
3:11
But I do know how to follow examples and get it working the way I want it to
3:16
So if you do decide to do this, hopefully this will help you in that endeavor. Now the thing about using dynamic parameters is your function or script logic has to be contained in begin process in block I have a video where I gone over that before I include a link to it down below and up in the corner here in a second
3:34
I have commented out here on lines 80 through 90, just an example of what Begin process looks like
3:41
One thing I also want to point out is your dynamic parameter. You won't be able to reference it just using the dollar sign variable nomenclature that you would normally use for your parameters
3:50
What I like to do is just create a variable of my own with the name
3:54
and then you look at the automatic variable PS bound parameters and assign at the value of private channel owner
4:00
because that's been added to your bound parameters for the function. Now we're not actually going to have any code that goes through
4:07
and creates private channel standard channels. There's a whole Microsoft Teams PowerShell module that can do that type of thing for you
4:13
When I first learned about dynamic parameters, this is the type of situation that came to my mind
4:18
when I was looking for an example in creating this code. All right, so let's take a look at our entire function put together
4:24
Let's go back through the components. We have our regular parameters, the channel name, and the channel type
4:29
And then starting on line 109, we are defining what our dynamic parameter looks like
4:33
And we're only going to return something if the channel type is private, meaning we have to specify a private channel owner parameter
4:41
Then on 9-125, we have our begin block, or just creating a variable to reference the private channel owner
4:48
and then starting our process block and saying if private channel owner, meaning it has a value
4:53
we're just going to output the channel name, the channel type, and the private channel owner
4:57
Otherwise, it's a public channel and we're just outputting the channel name and the channel type
5:02
We'll go ahead and highlight all of this and bring it into my session here
5:07
And let go ahead and see what this looks like I already got some examples here in my history We go channel name and we say my public channel
5:18
And if I go to channel type and I start hitting tab, it's going to go through my validate set and say private or standard
5:25
We'll start with standard to begin with. Now if I do a dash to do another parameter, start hitting tab, it's going to start going through the variables that are included when you add commandlet binding to your scripts or function
5:36
As you'll notice, there is no private channel owner parameter coming up in here
5:41
We'll just keep tapping through. And that's all looks as expected because we specified a standard channel to begin with
5:48
And we do that, we can see channel name, my public channel, channel type standard
5:54
Let's go do a private one here. My private channel. Do channel type
6:03
And now we do private. And if I do dash and start tabbing through the rest of my parameters
6:08
we now see private channel owner comes up. And I'll put just me, you know, whatever we need to put there and take that
6:16
And it goes into the other portion of our if statement showing we created a private channel
6:21
That does it for dynamic parameters. Hopefully you can see some use cases for this
6:25
Probably doesn't come up that often. You might be able to get away with doing things like parameter sets or just making parameters
6:31
mandatory or not mandatory. But when I learned about dynamic parameters, this situation came to mind of saying, you know, if I have one parameter with this value, then I want another parameter to appear
6:43
So I can add additional information when I run my script. Anyway, that does it for this video
6:48
Hopefully this helps you out and can just enhance your script or function writing capabilities
6:53
Thanks for watching. We'll see you next time
#Computers & Electronics
#Programming