0:00
What's up, everyone? Today we're going to talk about PowerShell Advanced Functions
0:03
Advanced functions allow you to take your normal functions and make them act like a commandlet
0:08
And this also gives you access to some advanced features. As always, this code is available out in my GitHub repository
0:14
I'll include a link to it down in the video description. My name is Jeff. This is Jeff Brown Tech. Let's go ahead and get started
0:20
First, let's go over the basic syntax of defining a function. We'll start here on line three
0:25
Use the function keyword. You then give the function a name. I do suggest following the verb dash noun commandlet formatting
0:33
but you don't have to. Then you have some curly brackets and everything inside the curly brackets there
0:39
to find what's in the function. Right now I have this function named New Greeting
0:43
just outputting Hello World to the screen. Notice there's no parameters, there's nothing else
0:49
This is the very basic syntax and definition for function. Now, if you want to enable advanced functions
0:56
you'll add the commandlet binding attribute. This enables your function to act like a compiled commandlet that's written in a dotnet language
1:04
something like C sharp. So notice here on line 9, we have the same function definition
1:10
On line 10, we have the commandlet binding attribute. On line 11, we then have an empty parameter block
1:16
This is required if you do add the commandlet binding attribute. You have to add in parameters, even if you don't have any defined for your function
1:24
And then the same output of just saying, hello world. Real quick, let's go ahead and highlight this, bring it into our current session, and just take a look at what this looks like
1:35
We'll just say new greeting, output that, and it's putting Hello World out to the screen
1:40
Now by adding the commandlet binding attribute, we have access to several properties that we can define in it
1:47
We can set Confirm Impact. This is specifying when the function should prompt for confirmation
1:54
We'll have an example of this down below. about default parameter set names
1:58
I've done a video on parameter sets, basically defining the default one if you have more than one
2:04
which one should the function use if it doesn't know which one it should be using
2:07
You can define the help URI to give the online version of the function help topic
2:12
In my last video, I talked about how to use the get help commandlet to get more information
2:18
about a command letter function. And one of them was the dash online to take you online This is where you define that There also supports paging I never used this before but if you returning data sets you can add a first skip or include total count parameters to your function
2:34
Support should process, you can add confirm or whatever parameters to the function
2:38
I have another video on this. I'll include a link to it down below
2:42
And then positional binding. It determines whether or not the function parameters are positional by default
2:47
This is enabled. Basically, the order that you define your parameters gives them positions, so you don't actually have to define them
2:55
For example, if I had a parameter here defined as name, it would have position zero because it was the first one in my list of defined parameters
3:04
So those are our commandlet binding attributes that we can work with if we add this to our functions
3:09
Let's take a look at a couple examples of those. First one is the support should process
3:13
This is going to add the what if parameter to the function. inside commandlet binding, just add support ship process that is setting it to true
3:22
We have a parameter here that is mandatory called name. And adding commandlet binding to your function gives you access to the PS commandlet variable
3:32
This is an automatic variable that contains lots of information about the commandlet or function that you're running
3:38
In this case, on line 35, we're saying if the PS commandlet include should process
3:44
basically you've added what if. It will just output what action we're going to take
3:49
If that's not true, then we're just going to output the greeting. So real quick, let's do a quick demo
3:54
This will add this in here. We'll say set greeting. And this just goes to Jeff
4:02
We'll put a name there. And it outputs Hello Jeff as expected
4:07
But we can also add what if. If we run this, it would just say we're performing this operation, set greeting on this target
4:15
You can customize this. what if message here. Again, I have another video about this
4:20
I'll include a link to it down below, so you get some more information about that
4:23
The what if parameter here is made possible through adding the commandlet binding to our function
4:29
and enabling support should process. Next we have Confirm Impact. Now confirm impact means when should the function prompt for confirmation that you want to make
4:40
a change. There are two components of this. First we have the automatic variable confirm preference Let me go ahead and highlight this Currently the value of this inside of my session is high And inside my function here set greeting I now have confirm impact equals medium
4:57
What I'm saying here is the impact of my function that I have is just medium
5:03
So medium is not equal to or higher than high. So if I were to run set greeting, it's not going to prompt for confirmation
5:11
Let's go ahead and verify that. Now we'll run Set Greetings, we'll take off the What If here
5:19
And you can see it goes through and just displace the message. However, if I change my impact to high
5:28
let's bring in this version here. And if I go back and run Set Greeting
5:35
it now comes back and says, Are you sure you want to perform this action? Yes, yes, to all, no, etc
5:40
Because I've defined my function as having a high impact and my current confirmation preference is also equal to high, it's going to prompt
5:51
If we were to go through and say my preference is medium and this is a low impact
6:00
We'll highlight all this. Actually, let's go through and cancel that one
6:03
and run this again. It's going to go through just fine because I have a low impact
6:14
and I only want to have a confirmation if it's medium or higher based on the current value
6:19
of confirmed preference here. The next one we have is the commandlet binding attribute gives you access to additional
6:27
write methods, one of those being something like write for both. Here you can output additional information
6:33
to the screen that's not included in your pipeline objects. Again, we have our new greeting function inside of commandlet binding
6:41
We don't have any other property set. If we take a look at our parameter, we do have our name parameter
6:47
but I do have a default value set to world for it if you don't specify a value
6:52
And then on line 68, we're using an if statement to say if our name parameter is equal
6:57
to world, which is the default value. We're just going to output a message to the console saying you're using the default value
7:02
Otherwise, we're going to say you're using a custom name value and then output our message here
7:07
So first let bring this into our session And now let just run new greeting on its own And it displays Hello World because World is the default value
7:21
But now if we were to go through and add Verboose to that
7:25
we get an additional message here saying you're using the default name value
7:29
and then Hello World. Same thing, let's go through and just demonstrate here
7:34
We'll put in Jeff, Hello Jeff, and then Verboose, and you're using a custom name value is our message now
7:43
Right verbose is something that's really good if you want to output additional information to the screen
7:48
when your function is running just to give more information maybe of what's going on in the background
7:54
and provides that information to whoever's running your function in case they're wondering what's going on
7:59
Maybe there's a long running process or you're wondering what action it's taking. Some other right methods include things like right warning and right error
8:07
if you're using error handling. I have another video on that on using tri-catch blocks
8:13
Those are the primary write methods I use when I've written scripts and functions in the past
8:17
but there are others and I'll include some link to some additional documentation on those down in the video description
8:23
The last thing I have here is I actually asked myself the question pretty recently
8:27
is what makes an advanced function, an advanced function. And when looking at the PowerShell documentation
8:32
it was pretty much by adding the commandlet binding attribute. However, if you look at other training materials online, maybe videos or blogs
8:40
when people talk about advanced functions, they usually incorporate some other things with them
8:44
because those are usually best practices and things you would see inside of compiled commandlets
8:49
Some of those other best practices include parameter validation. We've talked about that before
8:54
Being able to handle pipeline input and looking at error handling. For my point of view, from what I've read inside of documentation, the commandlet binding attribute
9:04
is what makes a function advanced. However, adding these other things are best practices
9:10
And what I'm going to do in the next video is I start to finish of how to write an advanced function
9:14
in order to solve a problem. I think it's really good to watch someone go through that process
9:18
if you've never written an advanced function and being able to see maybe my thought process
9:22
and to help you out in your script writing capabilities. Anyway, that does it for this video
9:27
just a quick introduction to advanced functions and the additional capabilities that they can give you
9:32
Like I said, in my next video, I'll go through writing an advanced function just to see that process
9:37
That's it for now. Thank you for watching. We'll see you next time