【问题标题】:ValidateScript ParameterAttribute in C# Binary PowerShell ModuleC# 二进制 PowerShell 模块中的 ValidateScript ParameterAttribute
【发布时间】:2015-01-12 18:31:01
【问题描述】:

我最近开始尝试使用 C# 进行二进制 PowerShell 编程,但在 ParameterValidationAttributes(主要是 ValidateScript 属性)方面遇到了一些问题。基本上,我想创建一个名为“ComputerName”的参数并验证当时计算机是否在线。在 PowerShell 中很容易:

    [Parameter(ValueFromPipeLine = $true)]
    [ValidateScript({ if (Test-Connection -ComputerName $_ -Quiet -Count 1) { $true } else { throw "Unable to connect to $_." }})]
    [String]
    $ComputerName = $env:COMPUTERNAME,

但我不知道如何在 C# 中复制它。 ValidateScript 属性接受一个 ScriptBlock 对象http://msdn.microsoft.com/en-us/library/system.management.automation.scriptblock(v=vs.85).aspx 我只是不确定如何在 C# 中创建它,我真的找不到任何示例。

[Parameter(ValueFromPipeline = true)]
[ValidateScript(//Code Here//)]
public string ComputerName { get; set; }

C# 对我来说很新,所以如果这是一个愚蠢的问题,我深表歉意。这是 ValidateScript 属性类的链接:http://msdn.microsoft.com/en-us/library/system.management.automation.validatescriptattribute(v=vs.85).aspx

【问题讨论】:

    标签: c# powershell binary powershell-module


    【解决方案1】:

    这在 C# 中是不可能的,因为 .NET 只允许编译时常量、typeof 表达式和属性参数的数组创建表达式,并且只有 string 以外的引用类型可用的常量是 null。相反,您应该从 ValidateArgumentsAttribute 派生并覆盖 Validate 以执行验证:

    class ValidateCustomAttribute:ValidateArgumentsAttribute {
        protected override void Validate(object arguments,EngineIntrinsics engineIntrinsics) {
            //Custom validation code
        }
    }
    

    【讨论】:

    • 感谢您提供的信息,这可能有点超出我对 C# 的概念。那么,在我创建该类之后,我可以在 ValidateScript() 中调用它吗? [ValidateScript({ValidateCustomAttribute.Validate(arg0, arg1)})]
    • @dotps1 您应用自定义属性而不是 ValidateScriptAttribute
    • 谢谢,我想我明白了,所以如果无法联系到计算机,我可以抛出异常吗?
    • @dotps1 是的,通过抛出异常,您通知 PowerShell 给定参数未通过验证。
    • 对我来说看起来没问题,但你应该注意Ping.Send 不会在目的地不可达时抛出,所以你应该检查返回值。
    【解决方案2】:

    只是用一个更完整的例子来扩展上面 user4003407 的答案。

    从 ValidateArgumentsAttribute 派生一个新的验证器并覆盖 Validate 以执行验证。验证是无效的,所以你真的可以在你选择的情况下抛出异常。

    Kevin Marquette 有一个出色的 article,但它在 powershell 中。这是一个 c# 示例:

    [Cmdlet(VerbsCommon.Get, "ExampleCommand")]
    public class GetSolarLunarName : PSCmdlet
    {   
        [Parameter(Position = 0, ValueFromPipeline = true, Mandatory = true)]
        [ValidateDateTime()]
        public DateTime UtcDateTime { get; set; }
    
        protected override void ProcessRecord()
        {
    
            var ExampleOutput = //Your code
            this.WriteObject(ExampleOutput);
            base.EndProcessing();
        }
    }
    
    class ValidateDateTime:ValidateArgumentsAttribute {
    protected override void Validate(object arguments,EngineIntrinsics engineIntrinsics) {
        var date = (DateTime)arguments;
        if( date.Year < 1700 || date.Year > 2082){
            throw new ArgumentOutOfRangeException();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 2023-03-24
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2015-11-30
      • 2018-02-21
      相关资源
      最近更新 更多