Understanding PowerShell Function Parameter Sets
Think you can do better? Show us!
When writing a PowerShell script, have you ever had a need to provide mutually exclusive parameters to a function? I mean those times when you want to allow one parameter but not another at the same time? Yea, me neither. I’m not sure why Josh decided to show us this anyway.
In this snip, we're going to look at parameter sets for powershell functions. This example function take some sort of content with that BV array a path to a file which can be read in. Or, a string value and then returns part of their content back to the console. If I load this function into memory. And then have a look at at syntax. This would indicate that I could run this function by providing either a path or a value or both at the same time, however, the functions been written such that if a path is provided this will overwrite the value so if I run the function providing a value. I received back that first 9 characters of this strong and if I provide a path to a text file containing a GUID I received the first 9 characters of that GUID. But if I run the function as the syntax indicates I should be able to by providing both a value and a path only the path is taken into account because that's how the functions written so we can go ahead and use parameter sets to compartmentalize our parameters ensure that the syntax for our functions accurately reflects the functions inner workings. We do this. By specifying the parameter attribute and within that. I'm supplying two arguments. The first being mandatory and the second being a parameter set name in this case. I've called this parameter set file. We do the same for our value parameter. However, this parameter sets been named string value and notice both parameters have been marked as mandatory. However, this only applies within. That particular parameter set this means if I want to use the file parameter set, I must specify a path and if I want to use the string value parameter set I must specify a value so if I load. This function back into memory and then have a look at its syntax now. I can see 2 possible ways of running this function one with the path and one with a value and notice. The lack of square brackets around those 2 parameters, indicating that they’re both mandatory so our previous example will still work. If I provide a value. And if I provide a path these both execute as they previously did. However, it's now invalid to provide both value and a path and we’re told if we try and do that that it cannot resolve that parameter set because that isn't a valid combination of parameters. And. If I wanted to go ahead and provide just a string to my function without providing a parameter. Name I’ll also get an error stating that a parameters set can't be resolved I can make this a valid way of executing my function. By telling PowerShell what my default parameter set should be using the commandlet binding attribute and the default parameter set name argument and I’ve specified that the default parameter set should be string value. Now also specify the value parameter is for the value parameter is position zero. This just means I don't have to provide that parameters name. In order to use it. So if I load my function back into memory and look at the syntax here again. Notice that these square brackets around the name value, but not the parameter overall, meaning that supplying that name is now optional. So if I go back to my last example this now runs as you would expect because I've effectively provided a value without having to tell powershell that I'm providing a value explicitly. That’s been Parameter sets for powershell functions. Thank you for watching.