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

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 1 year, 6 months ago.

  • Author
    Posts
  • #26938
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    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
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Disregard, I got it.

    can't use the mandatory with validatescript apparently

  • #26941
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #26942
    Profile photo of Flynn Bundy
    Flynn Bundy
    Participant

    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
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    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
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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.

You must be logged in to reply to this topic.