Author Posts

July 20, 2015 at 5:07 am

Hi.

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.

July 20, 2015 at 5:17 am

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?

July 20, 2015 at 5:48 am

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.

July 20, 2015 at 6:06 am

@dave – Here is another option I'd seen before which uses a Regex comment to give a descriptive error message:

http://powershell.com/cs/blogs/tips/archive/2013/07/19/using-validatepattern-attribute.aspx

July 27, 2015 at 5:55 am

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".

[ValidatePattern("^VM\-Server\-([A-Z]{2,3})([0-9]{1,2})$|^W81([A-Z]{3})([0-9]{1,2})$")]

What do you think?