ValidatePattern Parameter Attribute


Would you be so kind and assist me?

I want to specify two possible patterns for my parameter $ComputerName in my Advanced Script. How do I write two regex-s? How do I separate them and enclose them with quotes and brackets?

Thank you very much for your help in advance.

Not sure that ValidatePattern could be specified twice for one parameter or that you could have it check against multiple patterns. It would probably be better to have a single pattern match. What are the possible computer names? Do you have any regex patterns built?

Regex has an “or” operator (the pipe symbol) that can be used to combine two patterns into a single regex. However, the ValidatePattern attribute produces error messages that are extremely unfriendly to users, and its use is generally discouraged. Using ValidateScript, you can get the same functionality, but also produce any error messages you like via the “throw” statement.

@Dave - Here is another option I’d seen before which uses a Regex comment to give a descriptive error message:

Thank you everyone for your assistance. Please excuse my late response.
I used the “|” pipe sign to combine two Regex-s because I needed it for my function which creates Gen2 VMs on my Hyper-V server and I wanted them to be according to my naming standards: for virtual servers i.e. “VM-Server-DC01” and for virtual workstations i.e. “W81Ent01”.


What do you think?