Using an "If" statement with the ValidateScript parameter validation attribute

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Simon Wåhlin Simon Wåhlin 1 year, 11 months ago.

  • Author
    Posts
  • #24232
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I would like to use an "If" statement within a ValidateScript parameter validation attribute for an advanced function I am writing instead of having to use the $PSBoundParameters variable to check if more than one parameter is being used.

    I would like to be able to use Method 1 rather than Method 2 if possible, but not sure if there is a way of doing this, because I keep getting the following error when using Method 1 below:

    [blockquote]Check-Parameters : Cannot validate argument on parameter
    'Param2'. The "If ($Param1){ Write-Warning -Message "The Param1
    parameter and the Param2 parameter cannot be used together!" }"
    validation script for the argument with value "10" did not return a result of
    True. Determine why the validation script failed, and then try the command
    again.[/blockquote]

    [u][b]Method 1[/b][/u]

    Function Check-Parameters
    { 
      
      [cmdletbinding[]]
    
      Param
      (
            $ComputerName = $env:COMPUTERNAME,
            [Switch]$Param1,
            [ValidateScript[({If ($Param1){Write-Warning -Message "The Param1 parameter and the Param2 parameter cannot be used together!" }}]]
            [Int]$Param2
      )
    
    Check-Parameters -Param1 -Param2 "10"
    }

    [u][b]Method 2[/b][/u]

    Function Check-Parameters
    { 
      
      [cmdletbinding[]]
    
      Param
      (
            $ComputerName = $env:COMPUTERNAME,
            [Switch]$Param1,
            [Int]$Param2
      )
    
    $ParamString = @(
                     
                $Params = @(    
                                Foreach ($Param in 'Param1', 'Param2')
                                { 
                                    If ($PSBoundParameters[$Param])
                                    {  
                                        $Param 
                                    } 
                                } 
                            )
     
                    Write-Output $Params)
    
         If (($ParamString -eq "Param1") -and ($ParamString -eq "Param2"))
         {
             Write-Warning -Message "The Param1 parameter and the Param2 parameter cannot be used     
             together!"
         }
    
         ElseIf (($ParamString -eq "Param1") -and ($ParamString -ne "Param2"))
         {
            "Param1 is being used!"
         }
    
         ElseIf (($ParamString -ne "Param1") -and ($ParamString -eq "Param2"))
         {
            "Param2 is being used!"
         }
    
         ElseIf (!($ParamString))
         {
            "No parameter is being used!"
         }
    }

    OR

    Should I use Parameter Sets?

    Thanks

  • #24235
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Is there a reason I am not receiving a response?

  • #24236
    Profile photo of Simon Wåhlin
    Simon Wåhlin
    Participant

    The ValidateScript has to return $true for the validation to pass.

    Here is a working example:

    Function Check-Parameters
    { 
      [cmdletbinding()]
      Param
      (
        $ComputerName = $env:COMPUTERNAME,
        [Switch]$Param1,
        [ValidateScript({If ($Param1){Throw "The Param1 parameter and the Param2 parameter cannot be used together!" }else {$true}})]
        [Int]$Param2
      )
      'Running function with parameters: {0}' -f ($PSBoundParameters.Keys -join ', ')
    }
    Check-Parameters -Param1 -Param2 "10"
    Check-Parameters -Param2 "10"
    Check-Parameters -Param1
    

You must be logged in to reply to this topic.