简体   繁体   中英

Powershell binary module - parameter validation

I'm learning PowerShell and one good friend recommended me this beautiful article about binary modules in PowerShell

I'm planning to migrate some of the console applications that I have created to be used as binary modules.

And one of the things that I'd like to do is validate my PowerShell parameters correctly.

I found this post:

http://huddledmasses.org/better-error-messages-for-powershell-validatepattern/

But I'm not sure if this is the approach that I should follow, I mean, I was even planning to use DataAnnotations to validate my PowerShell parameters in my binary module

In the near future I'm planning to start using more PowerShell directly, but in the meantime, I just want to convert my Console applications to PowerShell binary modules.

Could you guys point me in the right direction?

I just want to know if there is a more PowerShell way of doing things in a binary module to vlaidate my parameters

Jaykul's suggestion is more of: how to get new validators for my PowerShell code. Instead, I would start with build-in ones and do extra work only if needed.

You will find a few in System.Management.Automation:

  • ValidateArgumentsAttribute
  • ValidateEnumeratedArgumentsAttribute
  • ValidateLengthAttribute
  • ValidateRangeAttribute
  • ValidatePatternAttribute
  • ValidateScriptAttribute
  • ValidateCountAttribute
  • ValidateSetAttribute
  • ValidateNotNullAttribute
  • ValidateNotNullOrEmptyAttribute

They are documented on MSDN, eg ValidateSet docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM