Parameter Validation to refuse wildcards

Welcome Forums General PowerShell Q&A Parameter Validation to refuse wildcards

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
9 months, 3 weeks ago.

  • Author
    Posts
  • #92636

    Participant
    Points: 56
    Rank: Member

    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

  • #92639

    Keymaster
    Points: 1,639
    Helping HandTeam Member
    Rank: Community Hero

    Sure, just put all that in a ValidationScript.

    • #92641

      Participant
      Points: 56
      Rank: Member

      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 🙂

  • #92743

    Participant
    Points: 56
    Rank: Member

    And worked it out.

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

The topic ‘Parameter Validation to refuse wildcards’ is closed to new replies.