3

I have recently begun experimenting with Binary PowerShell Programming in C#, and I am having some trouble with ParameterValidationAttributes, the ValidateScript Attribute mostly. Basically, i want to create a Param named "ComputerName" and validate the computer is online at that time. It was easy in PowerShell:

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

But i cannot figure out how to replicate that in C#. the ValidateScript attribute takes a ScriptBlock object http://msdn.microsoft.com/en-us/library/system.management.automation.scriptblock(v=vs.85).aspx im just not sure how to create that in C#, and i cannot really find any examples.

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

C# is very new to me, so i appologize if this is a dumb question. here is a link the ValidateScript Attribute Class: http://msdn.microsoft.com/en-us/library/system.management.automation.validatescriptattribute(v=vs.85).aspx

2 Answers 2

7

It is not possible in C#, since .NET only allow compile time constants, typeof expressions and array creation expressions for attribute parameters and only constant available for reference types other then string is null. Instead you should derive from ValidateArgumentsAttribute and override Validate to perform validation:

class ValidateCustomAttribute:ValidateArgumentsAttribute {
    protected override void Validate(object arguments,EngineIntrinsics engineIntrinsics) {
        //Custom validation code
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the information, this maybe a little out of my conception of C#. So, after i create that Class can i just call it in the ValidateScript()? [ValidateScript({ValidateCustomAttribute.Validate(arg0, arg1)})]
@dotps1 You apply your custom attribute instead of ValidateScriptAttribute.
Thanks, i think i understand, so i can just throw an Exception if the computer cannot be contacted?
@dotps1 Yes, by throwing exception, you notify PowerShell that given argument does not pass validation.
It looks ok for me, but you should be noted that Ping.Send does not throw if destination unreachable, so you should check returned value.
|
0

Just to expand on user4003407's answer above with a more complete example.

Derive a new validator from ValidateArgumentsAttribute and override Validate to perform validation. Validate is void so you really just get to throw exceptions in cases you choose.

Kevin Marquette has an excellent article but it's in powershell. Here is a c# example:

[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();
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.