Author Posts

February 1, 2018 at 4:17 am

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

February 1, 2018 at 4:21 am

Sure, just put all that in a ValidationScript.

February 1, 2018 at 4:51 am

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 🙂

February 1, 2018 at 9:41 pm

And worked it out.

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