Author Posts

June 29, 2015 at 2:49 pm

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

June 29, 2015 at 2:55 pm

Disregard, I got it.

can't use the mandatory with validatescript apparently

June 29, 2015 at 2:57 pm

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

June 29, 2015 at 3:16 pm

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?

June 29, 2015 at 5:35 pm

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.

June 30, 2015 at 3:30 am

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.