From the course: PowerShell: Functions for Advanced Automation

Advanced functions and common parameters

From the course: PowerShell: Functions for Advanced Automation

Start my 1-month free trial

Advanced functions and common parameters

- [Instructor] PowerShell is an incredibly versatile tool for administering your Windows network. Some PowerShell scripts, however, can become a bit rigid. As their designed to serve very specific purposes. Advance functions in PowerShell can add flexibility to a script. They're still serving a single purpose, but they can be made more or less specific in scope by the use of parameters. Advanced functions work more like some of the commandlets that you're already familiar with. Consider the commandlet, 'Get Service.' If I were to just run that commandlet, it's going to return a list of all of the services on my machine, with their name, display name, and their current status. I can get a more specific set of results if I add some parameters. For example, let me scroll up, if I once again type, 'Get Service,' and then add to the end of that, the perimeter of name event log, now I can see the details of just that specific service. I could even go further and look at some other data about the event log service by adding the parameter, 'dependent services.' By simply adding parameters to an existing commandlet, I can get a more specific, and therefore, more useful set of data. Advanced functions allow you to accept parameters to modify how your functions run. This is done through the use of two expressions at the very beginning of your function. The first is, common parameters. There are some parameters that PowerShell makes available to all commandlets. Things like, adding an error action, or choosing verbose output. You can add these common parameters by simply placing this line at the very beginning of your function, 'commandlet binding,' followed by open and closed parenthesis, all encased in square brackets. The param with open and closed parentheis is something we'll take a look at later. Lets take a look at how this works in PowerShell ISE. Here we are, back on our server. So lets switch over to ISE, and here, I'm going to create a new funtcion, By just typing, function. And lets call our function, 'not advanced.' Now, to actually have a function, we need to have an open and closed curly brace. I guess I don't need to say curly, this is a brace, these are brackets, and these are parenthesis. However, referring to these as curly braces, these as square brackets, and parenthesis does help a little bit to keep them separate. So, let me get rid of the extra characters, and put a couple extra lines for whatever else we may need to do. Now I'm going to load this function, which clearly doesn't actually serve a purpose, but I'm going to select it and click on the run selection, so that this function is placed into memory. Now, if I go down to the terminal, and type, 'not advanced' We have that function, we could run it, and it would do nothing because that's all the function is designed to do, but if I hit a space and then type the hyphen key, you'll notice there are no suggested parameters. That's because PowerShell isn't aware of any parameters that work with, 'not advanced.' So, let me back this out, and lets create a different function. This time, lets call it, 'now advanced.' Open the braces, give us some working space, close the braces. Now here, inside our working space, indented a little bit, I'm going to add the expression that gives us the common parameters. Commandlet binding followed by open and closed parenthesis, all encased in the square brackets. I'm going to go ahead and add the param at the end as well. Notice, I'm not specifying any parameters. I haven't given any detail about commandlets that I want bound. I'm just adding that line, all on it's own. Now, if I select the entirety of this function, and click run selection to load that function into memory, now lets see what happens when I begin to call this function. After I type in, 'now advanced' and hit space, when I hit the hyphen key, there's a list of common parameters that are readily available to me. They're there, suggested for me to select from. Now, I'm not going to choose any of those right now, but lets take a look back up here at our function, and in the parameters parenthesis, I'm going to add a custom parameter that we might want to have in our function. I'm going to add a variable of my number, and we have the suggestions offered to us by PowerShell ISE as we type. You don't have to use them, but be careful of using the tab key while you're typing. It just may select one of these suggested items. Now, let me go ahead and select our function, run the selection, and in the terminal pane I'm going to scroll it up a bit here. And once again, let me go ahead and type, 'now advanced.' Followed by the hyphen, we can see our custom parameter has been added to the list, in addition to all of the common ones. The addition of common arguments and custom parameters, is what defines an advanced function in PowerShell. As we continue, we will take a look at how to put these two advanced features to work.

Contents