Use all parameters at one

i wrote a method with different construct

static[psobject] find(

[string]$keywords

)
{



$places= import-clixml c:\ex-sys\xml\place.xml

return $places|where-object {$_.name -like "*$keywords*" -or $_.type -like "*$keywords*"}

}
static[psobject] find(
[string]$name,
[string]$type



)
{

    $places= import-clixml c:\ex-sys\xml\place.xml
    
    return $places|where-object {$_.name -like "$name" -and $_.type -like "$type"}
}

i would like to call the two in different situations
input one parameter

.\script\gps.ps1 find town

input two parameters

.\script\gps.ps1 find KTV town

i would like powershell to find out how many parameter i input and call the method for me

Is there a variable we could use to do such thing?

In advanced functions you have ValueFromRemainingArguments parameter attributed. You can designate one of your parameters to take the unbound, remaining arguments. Unfortunately, this is not available to classes at the moment. You could wrap the method calls with a function that utilizes this.

The ValueFromRemainingArguments property - Mastering Windows PowerShell Scripting - Third Edition [Book].

Do you need a class for this? Native PowerShell using Parameter Sets might be a better approach. Example:

function Find-Place {
    [CmdletBinding(DefaultParameterSetName = 'Keyword')]
    param (
        [Parameter(ParameterSetName = 'Keyword', Mandatory = $true)]
        [String]
        $Keyword,
        [Parameter(ParameterSetName = 'ByName', Mandatory = $true)]
        [String]
        $Name,
        [Parameter(ParameterSetName = 'ByName', Mandatory = $true)]
        [String]
        $Type
    )

    $Places = @(
        [PSCustomObject] @{
            Name    = 'Brighton'
            Type    = 'City'
        }
        [PSCustomObject] @{
            Name    = 'Scarborough'
            Type    = 'Town'
        }
        [PSCustomObject] @{
            Name    = 'Cardiff'
            Type    = 'City'
        }
    )
    
    [ScriptBlock]$Filter = switch ($PSCmdlet.ParameterSetName) {
        'Keyword' { { $_.name -like "*$Keyword*" -or $_.type -like "*$Keyword*" } }
        'ByName' { { $_.name -like "$name" -and $_.type -like "$type" } }
    }

    $Places | Where-Object -FilterScript $Filter
    
}

Find-Place -Keyword 'Town'
Find-Place -Name 'Cardiff' -Type 'City'
3 Likes

100% agree with Matt. - in this case Parameter set would work really well.

that is really good,didn;t know the parameterset .but i want to call different construct in function by different input i give
parameterset could also solve it but i want to try with class

from Property ParameterAttribute.ValueFromRemainingArguments Property (System.Management.Automation) | Microsoft Learn
i found the solution for my problem
for example :

    function find {
    
        param (
        
            $Param1
            ,# Parameter help description
    
            $Param2
        )
      
    
    switch (  ($MyInvocation.BoundParameters).count) {
        1 { [place]::find($param1) }
        2 {[place]::find($param1,$param2)}
    }
    
    }
    }
gps.ps1 town
gps.ps1 KTV town


Awesome glad @krzydoug 's docs helped. Definitely a clever use of the $Myinvocation variable and the boundparameters property. If anyone else (or even yourself) plan on using this code in the future, I might suggest adding a comment explaining the context behind the code. I find that comments help a ton when we have solutions to problems and it’s not always straight forward as to why we went with this.

I have gone through this post and found it very much useful and informative. Thanks for sharing these insights mate.

glad to heard that ,thanks for the respend

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.