Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:09:22+00:00 2026-06-07T05:09:22+00:00

Consider this: Function Foo { param( #???? ) } I want to call Foo

  • 0

Consider this:

Function Foo 
{
    param(
        #????
    )
}

I want to call Foo like this:

Foo -Bar "test"

Without it blowing up that I don’t have a $bar param specified. Is that possible? 🙂

Update:

I want this to work:

Function IfFunctionExistsExecute
{
    param ([parameter(Mandatory=$true)][string]$func, [parameter(Mandatory=$false)][string]$args)
    begin 
    {
        # ...
    }
    process
    {
        if(Get-Command $func -ea SilentlyContinue)
        {
            & $func $args   # the amperersand invokes the function instead of just printing the variable
        }
        else
        {
            # ignore
        }       
    }
    end
    {
        # ...
    }
}


Function Foo
{
    param([string]$anotherParam)
    process 
    {
        $anotherParam
    }
}

IfFunctionExistsExecute Foo -Test "bar"

This gives me:

IfFunctionExistsExecute : A parameter cannot be found that matches parameter name 'Test'.
At C:\PSTests\Test.ps1:35 char:34
+ IfFunctionExistsExecute Foo -Test <<<<  "bar"
    + CategoryInfo          : InvalidArgument: (:) [IfFunctionExistsExecute], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,IfFunctionExistsExecute
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:09:25+00:00Added an answer on June 7, 2026 at 5:09 am

    I would suggest two alternatives.

    First: you may want to consider passing whole function + it’s parameters as scriptblock parameter to your ifFunction…

    OR: use ValueFromRemainingArguments:

    function Test-SelfBound {
    param (
        [Parameter(
            Mandatory = $true,
            HelpMessage = 'Help!'
        )]
        [string]$First,
        [Parameter(
            ValueFromRemainingArguments = $true
        )]
        [Object[]]$MyArgs
    )
    
    $Arguments = foreach ($Argument in $MyArgs) {
        if ($Argument -match '^-([a-z]+)$') {
            $Name = $Matches[1]
            $foreach.MoveNext() | Out-Null
            $Value = $foreach.Current
            New-Variable -Name $Name -Value $Value
            $PSBoundParameters.Add($Name,$Value) | Out-Null
        } else {
            $Argument
        }
    }
        $PSBoundParameters | Out-Default
        "Positional"
        $Arguments
    
    }
    
    Test-SelfBound -First Test -Next Foo -Last Bar Alfa Beta Gamma
    

    In this case I use $MyArgs to store everything besides my mandatory parameter ‘First’. Than some simple if will tell me if it’s named parameter (-Next, -Last) or positional (Alfa, Beta, Gamma). This way you can have both advantages of advanced functions binding (whole [Parameter()] decoration) AND leave room for $args-style parameters too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Consider this javascript code: var bar = function () { alert(A); } var foo
Consider the following to JavaScript snippets: function foo() { this.bar = function() { };
Now, I usually call a function (that requires no arguments) with () like this:
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
Consider this scenario. I have my own website, that I use as my identifier,
Consider this code: function Foo(ds as OtherDLL.BaseObj) dim lngRowIndex as long dim lngColIndex as
Consider the following function: public function foo(bar1:int, bar2:uint, bar3:String, bar4:Boolean):void{} What I want is
Consider foo.sh: #!/bin/bash function foo() { source another.sh echo This shouldn't be executed. Return
Consider this example Javascript code: a = new Array(); a['a1']='foo'; a['a2']='bar'; b = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.