Parameter Validation to refuse wildcards

Hi All,

Is there a way in the parameter section of a function to refuse wild cards.

I have found i can use this to put at the top of the function but would like to deal with it at the parameter level rather than in the function itself.

if ([System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Computer)) {
    Write-Error -Message "Get-VAppAndVMData does not accept wildcard characters" 
    Throw
}

Thanks

Sure, just put all that in a ValidationScript.

So i’ve added

[ValidateScript({if ( [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($ComputerName)){Throw}} )]

And it fails for normal and wildcard searches. From what I’ve read it wants some kind of Boolean. I’ve tried

[ValidateScript({if ( [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($ComputerName)){$false}} )]

And it fails for normal and wildcard searches.
I also tried just adding in the 1st post completely.

[ValidateScript({if ([System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Computer)) {
            Write-Error -Message "Get-VAppAndVMData does not accept wildcard characters" 
            Throw
        }} )]

And that also told me to go away. Brain is slowing down at the end of the day, might have to revisit in the morning :slight_smile:

And worked it out.

[ValidateScript({-not[System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($_)})]