Validating user input in shell script
Used to exercise the pattern recognition engine, which can be used with the reader interface to the parser.
But thatâ€™s enough to allow us to delete the â€œif $Event Date â€“is [Date Time]â€ from the If statement and from the error message. If the parameter value takes a collection, like a list of dates, and any one of the dates fails the test, Validate Script throws an error that stops the script, even if all other dates pass the test. (Todayâ€™s date would generate the same error.) The error message explains (in more words that I could use) that the date failed the validation test.These attributes are statements that are added to the parameter definition.They tell Windows Power Shell to examine the parameter values that are used when the function is called and determine whether the parameter values meet some specified conditions.If the value of the $Event Date parameter is a Date Time object and itâ€™s later than now, the function writes a nice sentence with the date to the console or host program. I can never remember this, so I use an ISE snippet or copy it from about_functions_advanced_parameters.But, if the value of $Event Date is not a Date Time object, or itâ€™s not a future date, the function generates an error. But aside from the syntax, Validate Script is easy to use.
Before we get to the details, letâ€™s talk about why youâ€™d want to use something like this. Instead, the Windows Power Shell engine tests the parameter value and you can use the script to do scripty things. Hereâ€™s a silly function that will serve as our example. Validate Script is enclosed in square brackets: [Validate Script].