简体   繁体   English

Powershell二进制模块 - 参数验证

[英]Powershell binary module - parameter validation

I'm learning PowerShell and one good friend recommended me this beautiful article about binary modules in PowerShell 我正在学习PowerShell, 一位好朋友向我推荐了这篇关于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. 我想做的其中一件事就是正确验证我的PowerShell参数。

I found this post: 我发现这篇文章:

http://huddledmasses.org/better-error-messages-for-powershell-validatepattern/ 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 但是我不确定这是否是我应该遵循的方法,我的意思是,我甚至打算使用DataAnnotations在我的二进制模块中验证我的PowerShell参数

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. 在不久的将来,我计划直接开始使用更多的PowerShell,但与此同时,我只想将我的控制台应用程序转换为PowerShell二进制模块。

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 我只是想知道在二进制模块中是否有更多的PowerShell方式来处理我的参数

Jaykul's suggestion is more of: how to get new validators for my PowerShell code. Jaykul的建议更多:如何为我的PowerShell代码获取新的验证器。 Instead, I would start with build-in ones and do extra work only if needed. 相反,我会从内置的开始,只在需要时做额外的工作。

You will find a few in System.Management.Automation: 你会在System.Management.Automation中找到一些:

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

They are documented on MSDN, eg ValidateSet docs . 它们记录在MSDN上,例如ValidateSet文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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