In the validation script the value of the variable is passed as in a pipeline.
You have to use $_
This test you want to perform is ideal for a try-catch setup.
A validation script has to return $true or $false.
But any object, also a Datacenter object, besides $null is considered $true, so you can just use the result of the Get-Datacenter.
With the catch block, nothing, or $null, is returned.
Hence the validation fails.
I normally test this with a short test function, to which I then feed all possible combinations.
A short example (adapt the values on the parameter according to your environment).
{
[CmdletBinding()]
param(
[Parameter(Mandatory,HelpMessage="Enter a Datacenter Name")]
[ValidateScript({
try{
Get-DataCenter-Name $_-ErrorAction Stop
}
catch{
throw"Datacenter $_ not found. Please enter a valid Datacenter Name."
}
})]
[String[]]
$DCChoice
)
$DCChoice
}
Test-Validation-DCChoice validDC
Test-Validation-DCChoice invalidDC
Test-Validation-DCChoice validDC,validDC
Test-Validation-DCChoice validDC,invaliddc
Test-Validation-DCChoice invaliddc,invaliddc