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
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?
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.