0

Given:

  • PowerShell 5.1
  • Azure DevOps Server 2019

I'm trying to call my function directly from my Azure PowerShell Task Arguments, is this possible? I'm not getting any expected output.

param([String] $Name, [Int] $Age, [String] $Path)

Function Foo
{
    Param(
        [String]
        $Name
    ,
        [Int]
        $Age
    ,
        [string]
        $Path
    )
    Process
    {
        write-host "Hello World"
        If ("Tom","Dick","Jane" -NotContains $Name)
        {
            Throw "$($Name) is not a valid name! Please use Tom, Dick, Jane"
        }
        If ($age -lt 21 -OR $age -gt 65)
        {
            Throw "$($age) is not a between 21-65"
        }
        IF (-NOT (Test-Path $Path -PathType ‘Container’))
        {
            Throw "$($Path) is not a valid folder"
        }
        # All parameters are valid so New-stuff"
        write-host "New-Foo"
    }
}

enter image description here

Update 3

enter image description here

Update 2

enter image description here

Update 1

enter image description here

2
  • Please add the code as code tags in your question Commented May 19, 2022 at 13:11
  • Updated OP with code tags Commented May 19, 2022 at 13:12

1 Answer 1

1

If you execute your script directly, it will simply define the Foo function, but never call it.

Place the following after the function definition in your script in order to invoke it with the arguments that the script itself received, using the automatic $args variable via splatting, which allows you to pass arguments via a variable containing an array or hashtable of parameter values, which needs to be referenced with @ rather than $:

Foo @args

The alternative would be not to invoke a script file, but a piece of PowerShell code (in PowerShell CLI terms, this means using the
-Command parameter rather than -File), which would allow you to use ., the dot-sourcing operator to first load the function definition into the caller's scope, which then allows it to be called:

. "$(System.DefaultWorkingDirectory)/_RodneyConsole1Repo/FunctionExample.ps1"
Foo -Name Rodney -Age 21 -Path ""
Sign up to request clarification or add additional context in comments.

10 Comments

I updated OP with latest results. Not sure what I'm doing wrong?
@Rod, if you pass "Tom", "21", "", you're passing a 3-element array as a single argument; to pass 3 separate arguments (positionally), separate them with whitespace: "Tom" "21" ""
Please see Update 2 in OP
you're absolutely correct! Take a look at word Container in OP code. It is surrounded by special quotes, smh. Thanks for sticking with me.
@Rod, splatting is a way of passing arguments that bind to parameters. It doesn't define variables.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.