Validate PowerShell Parameters Today!
21K views
Apr 24, 2024
Learn 5 different ways to validate parameter input in your PowerShell scripts! Getting Started with PowerShell Parameters https://youtu.be/b8ga_PwIVvc 00:00 Introduction 00:25 Script Overview 01:06 ValidateLength 02:44 ValidateSet 07:47 ValidateRegex 10:07 ValidateScript 12:46 ValidateRange 14:11 Summary
View Video Transcript
0:00
What's up everyone, today I want to continue talking about PowerShell parameters and how you can validate the input to your parameters to make sure your script has the right data in order to execute correctly
0:11
This is a follow-up video where we went over an introduction to parameters and how to define them and make them mandatory and put data types on them
0:20
I'll put a link to that video down in the video description. Let's go ahead and get started
0:25
Here I have a script called New Voter Registration. It takes in several pieces of information through parameters and at the very bottom we're just going to output a voter registration record
0:37
Some of these parameters include a name, a zip code, party affiliation, email address, date of birth, and age
0:45
Right now all of them are mandatory so we have to specify a value for each one and they all have their data types
0:54
Right now we could run the script and input all sorts of different information that would not make for a very good voter registration record
1:02
We're going to go through several different methods on how to validate each one of these
1:07
To get started I've commented out each of the other parameters and we're going to focus on them one at a time and add different validation methods to them
1:15
The first one we have here is name. Basically I just want to make sure that somebody inputs a name and it's of a certain length, maybe 3 or 4 characters
1:26
and we'll put in a maximum in there just so it doesn't fill up the entire name field
1:33
To get started with this we can put in some square brackets and do validate length
1:38
Now each of these validation methods are going to have parameters that we can actually pass to them of the information that we want to do
1:46
Those will be enclosed in parentheses. In this case we want to set a minimum and maximum length
1:51
Let's just say maybe a minimum length of 3 and a maximum of 35
1:56
We'll go ahead and save this. And let's start trying out our new voter registration here
2:04
I'm going to do name and Jeff Brown. And we see that took that successfully and our voter registration record has its first piece of information in it
2:14
If I were to go back and just do 2 characters it's going to come back and say the character length of 2 is too short, it has to be at least 3
2:25
And then if we put in a whole bunch of characters and we go over 35 it'll come back and say it has to be fewer than or equal to 35 characters
2:36
Validate length, very straightforward. Just a way to make sure it has some type of value and if you have a minimum or a maximum
2:44
The next parameter is zip code. What we want to do is make sure we put in a valid zip code that corresponds with our voter registration area
2:52
Maybe there's only specific zip codes that you're registering for. I'm going to validate the zip code using specific ones using validate set
3:00
And again square brackets, validate set keyword and we have parentheses, some things to pass into it here
3:07
And we're just going to enclose these in quotes and put in some valid zip codes here
3:14
So right now we're saying the value that we put in for zip code, the string has to match one of these inside of this set
3:27
So let's continue with our example. Let me pull back up our successful command here
3:34
We'll say zip code. Now what's interesting here is now that we have validate set, what we can do is when we get the zip code we can start hitting tab
3:46
And it's going to tab through the values that we have within our validate set
3:51
This is super handy if someone doesn't know what value they need to put in here
3:56
When you do validate set, it's just going to go through and show them what options they have
4:01
Same thing if we have a partial entry and we press tab, it's going to complete to the nearest one inside the validate set that matches it
4:08
So this is perfect. What we'll do here is press enter. We've got that going for us
4:13
And if we were to put in a zip code that doesn't match, it'll come back and say it is not in the set that we have defined here
4:23
Another option we probably could also add validate length on here to make sure it is five digits long
4:29
But that's kind of taken care of by validate set. But if you didn't have a specific set of zip codes, you weren't worried about that
4:36
You could do validate length and make sure it's at least five characters and probably a maximum of, what, ten if you do a dash and the other four characters in a zip code
4:46
Just showing you how you can maybe mix and match these different validation methods
4:50
Okay, next we have party affiliation. We want to make sure we can only put in specific parties when we're registering a new voter here
4:58
And we're going to continue using validate set, but we're going to add a couple of extra things to it
5:04
So let's do validate set. And we're going to say our parties are green, yellow, orange, and red
5:19
So there is our validate set for our party affiliation. The exact same thing here if we bring this up and do party affiliation
5:29
Let's make sure to save the script first so it shows up. We'll do party affiliation
5:34
And again, I can tab through and start seeing our various values that are available to us
5:41
Let's go back and actually put in a valid zip code here
5:49
And same thing if I start typing yellow or put it that, it's going to autocomplete to that
5:53
And we've got a good entry there. Now one thing we have going on here, you notice we have capitalization
5:59
And maybe we want to make sure people follow this capitalization. Right now I can come in here and do lowercase and it comes back
6:08
But maybe that's not what we want. We want it to look more official and everything
6:12
So within our validate set, we have an option to tell it to ignore the case or not
6:19
So I'm going to go on a separate line here. And we have an option of ignore case
6:26
And we're going to say this is false, meaning we don't want to ignore the case
6:30
It has to match exactly what we've put into our validate set. So we'll save that there
6:36
Let's bring back up our yellow option. And it's going to come back and say
6:40
argument yellow does not belong in this set specified by the validate set attribute
6:45
Now that might be a little confusing because you say, hey, I did put in yellow
6:49
Okay, it's not capitalized, whatever. But it is in that set. We can also add a custom error message
6:56
So let's go down here. I'm going to add an error message equals
7:01
Let me go copy my error message I have over here. And our custom error message is the item
7:07
And we have string interpolation or replacement here. The first one that passes in is the one you selected is not part of the set
7:15
The second one, or curly brackets one, is the full set. Or it does not match capitalization rules
7:22
So we'll save this. Let's go back. We'll rerun with a lowercase y
7:27
And our error message is just a little bit more informative. It's not part of this set or does not match the capitalization rules
7:33
Hopefully that would help them out in understanding that they need to match
7:37
exactly what we have in here. And now we have a correct entry there
7:43
And it matches the case and a custom error message. Moving right along, the next one we have is email address
7:50
We want to have some contact information in there, you know, about upcoming elections or anything like that
7:56
And we want to make sure that we put in a valid email address. Valid email address is the first part of it, an at sign, and then something dot something
8:04
for the domain name, example.com or whatever. Here we can actually take something like this that has a common format
8:12
and validate it against regex. So let me go copy my pattern here
8:19
What we have here is the validate pattern keyword. And in the parentheses is regex for an email address
8:26
I just got this off the internet. There's lots of examples out there. Maybe it's social security number, credit card numbers, whatever
8:33
There's probably regex out there that someone else has figured out for you if you're not good at regex
8:38
We have the first part of it, you know, any character at any character dot the rest of it
8:45
So, perfect. Let's go ahead and save this and give this a try here
8:49
We're going to say email address. I'll say someone at somewhere dot com. Perfect
8:56
It took that. But let's say I missed the dot here for dot com and I don't have the full domain name
9:02
It will come back and say does not match this pattern, supply one that matches this pattern
9:08
Now that looks really confusing. If somebody doesn't know regex, they could look at that and say
9:13
I don't know what you're looking for here. What do you mean does not match this pattern
9:17
Again, we can put in a custom error message. Copy an error message here
9:24
Clean up some formatting. Okay, so here's an error message. Again, the curly bracket zero here is the value of the parameter coming in to the validate pattern
9:37
We'll just say it's not recognized as a valid email address. And that would leave it up to the person to figure out what's wrong with their email address format
9:46
Let's save that. We'll try this again. And you can see here our message
9:52
Someone at somewhere com. There's no dot there. It's not recognized as a valid email address
9:57
A little more informative. It doesn't throw the regex pattern out there, especially if it's longer or more complicated
10:03
and just gives the user of your script a little more information on what you're looking for
10:08
The next parameter we're looking at is date of birth. When registering someone, you might take in their birth date
10:15
and want to validate that they are actually old enough to vote. In the United States, it's 18
10:20
It could vary elsewhere. This example here is probably the most powerful because we can actually run a short PowerShell script
10:28
against the value put in for the parameter to see if it's valid or not
10:33
Here I've gone ahead and copied my validate script and pasted it in
10:37
On line 28, we have the validate script keyword, and then we have parentheses
10:42
And then the script itself needs to be enclosed in curly brackets
10:47
And you can put any number of things in here. Ideally, you want to keep it short
10:51
Otherwise, your parameter definition is going to get really long. This one's actually pretty long, longer than what I thought it would be
10:57
But it's just a regular PowerShell script. We have $today equals get date, so that's the current date
11:03
We have their date of birth, get date. And in the date parameter, you see $underscore
11:08
The $underscore represents the value the person put in for their parameter
11:15
Pretty much, it's the current object in the pipeline. It's passed over to validate script, and that's just what it represents
11:23
You probably often see that before if you're doing a for each object in a pipeline
11:27
and you have your curly brackets for your script block and you represent the current object with a $underscore
11:33
The exact same thing. We're going to do a date difference here on getting their age
11:37
and just double-checking, you know, is it the same month, the same day
11:41
and do some calculations there. Line 35, we have an if statement
11:45
If the age is less than 18, we're going to throw an error
11:49
so the script will exit, saying you must be 18 to register to vote
11:55
Otherwise, if it is a valid age, you have to return true
12:00
When you're doing a validate script like this on a parameter, at some point if the value is a good value, you validated it
12:08
you have to return true, or the validate script property here just does not work
12:14
If the validate script determines the value is not a good value according to your parameters, you have to throw an error
12:20
If it is, you have to return true. So we've got that saved there. Let's go ahead and bring back our command here
12:27
Let's do date of birth. We'll do 1-1-1970. That should be old enough
12:35
Perfect, no errors. But if we come back and do a much later date that's less than 18 years
12:41
it's going to come back, cannot validate, you must be 18 to register to vote
12:47
Our last parameter is age. It's a little bit of a duplicate of date of birth
12:51
but I had one more validation method that I wanted to demonstrate
12:55
and the best example I could use is age. The one here that we're going to look at is a validation of range
13:02
If you have a number like age, which I have the data type as integer
13:07
you can make sure what they're putting in there is within a valid range
13:12
Again, let's look at validate range attribute. The first parameter you pass into this method is the minimum, so we'll say 18
13:22
and then we'll do the max of maybe 120. Hopefully no one over 120 tries to register
13:28
but we can update that later if we need to. Pretty straightforward. We bring this up here
13:34
We'll do age. Let's say 25. Perfect. Let me fix my birthday here so we can actually test that
13:43
Let's go back to 2000. Perfect. Showed the age there. No problem
13:48
If we come back and do something like just 17, it will throw the error
13:53
It's less than the minimum allowed range of 18. Supplied argument, that is greater
13:57
We could probably do a custom error message here saying you must be 18 to vote
14:01
just like we did on other things. But just a quick example of how you could use something like validate range
14:06
to make sure the number that somebody is putting in is within the range you're expecting for your script
14:12
All right. That does it for this video. Hopefully you've learned something here on how you can validate the input on your parameters
14:19
There's always more options to do here, but these are a lot of the basic ones, I think, to get you started so you can validate the input there
14:28
Thank you for watching, and we'll see you next time
#Computer Education
#Educational Software
#Programming