How would I go about using [validatescript] to compare two parameters

Welcome Forums General PowerShell Q&A How would I go about using [validatescript] to compare two parameters

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
3 years, 6 months ago.

  • Author
    Posts
  • #26938

    Participant
    Points: 1
    Rank: Member

    How would I go about using [validatescript] to compare two parameters in an advanced function I am working on?

    For instance, I have two parameters, $ComputerNameOne and $ComputerNameTwo. I want to make sure that the $ComputerNameTwo parameter is not the same as $ComputerNameOne.

    I have tried this, but keeps erroring out on me:

    [Parameter(Mandatory = $true,
    			ValueFromPipeline,
    			ValueFromPipelineByPropertyName)]
    		$ComputerOne = $env:COMPUTERNAME,
    		
    		[ValidateScript({ If ($ComputerOne -ne $ComputerTwo) { Write-Output -Verbose "ComputerOne and ComputerTwo parameter values are not equal - Passed Validation!" }},
    			Mandatory = $true,
    			ValueFromPipeline,
    			ValueFromPipelineByPropertyName)]
    		[Alias('SC')]
    		$ComputerTwo

    Thanks

  • #26939

    Participant
    Points: 1
    Rank: Member

    Disregard, I got it.

    can't use the mandatory with validatescript apparently

  • #26941

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    I've used ValidateScript successfully with Mandatory. You might be misconstruing how your default value is being applied?

  • #26942

    Participant
    Points: 0
    Rank: Member

    hey mate,

    try something simple like:

    [ValidateScript({$computerone -ne $computertwo})]

    That throws an error for me. as per below:

     Verb-Noun -Param1 car -Param2 car 
    erb-Noun : Cannot validate argument on parameter 'Param1'. The "$param1 -ne $Param2" validation script for the argument with value "car" did not return a result of True. Determine why the validation script failed, and then 
    try the command again.

    After that you can pretty it up and make it more robust. If you open the ISE and cress Ctrl+J the advanced function (complete) has all he validation sets with examples. Hopefully this helps. if not. what's the error you're getting?

  • #26943

    Participant
    Points: 1
    Rank: Member

    Okay, it seems to be working now.

    This is what I did:

    [Parameter(Mandatory = $true,
    			ValueFromPipeline,
    			ValueFromPipelineByPropertyName)]
    		[Alias('FC')]
    		$ComputerOne = $env:COMPUTERNAME,
    		
    		[Parameter(Mandatory = $true,
    			ValueFromPipeline,
    			ValueFromPipelineByPropertyName)]
    		[ValidateScript({ $_ -ne $ComputerOne })]
    		[Alias('SC')]
    		$ComputerTwo = $env:COMPUTERNAME,

    Thanks you two, I appreciate it.

  • #26948

    Participant
    Points: 438
    Helping Hand
    Rank: Contributor

    The variable computerone and computertwo, what are they actually for? Typically there is a source and a destination, so those variables are a bit unusual. I'm just curious.

The topic ‘How would I go about using [validatescript] to compare two parameters’ is closed to new replies.