1

I have a script tester.ps1; the first thing it does is call a function (defined within the script itself) called main.

I need to pass in the automatic variable $args that was passed into it from the commandline.

How do I do this?

The following doesn't seem to work:

#Requires -Version 5.0
#scriptname: tester.ps1
function main($args) {
    Write-Host $args
}
# Entry point
main $args

When I save this tester.ps1 and call it, the function doesn't see the passed-in parameter?

PS> . .\tester.ps1 hello world
From entry point: hello world
From Function:
4
  • 1
    Don't define $args as a param in your function: function main {write-host $args } Commented Nov 9, 2018 at 12:55
  • 1
    You can't use $args as an actual parameter; it's already a special value that always contains all the arguments to a function. In this case, simply making it main() is sufficient. (Passing $args to it is still required.) Saving $args outside the function in a global variable is another option, if you had many functions that needed access to all of them. Commented Nov 9, 2018 at 12:55
  • Thanks - but how then to 'stash' a copy of the $args as was set when I called the script. One reason to structure my script this way (with a 'main' function) was to try and avoid polluting variables back to the calling environment.... Commented Nov 9, 2018 at 13:43
  • Got it: I just renamed my function params to 'my_args'. Commented Nov 9, 2018 at 13:44

2 Answers 2

1

In your example, simply removing $args from the main function declaration, would be enough to have the output you want.

Though, be aware that if you want to pass parameters by name, you need to call main with the splatting operator @, for example:

#Requires -Version 5.0
#scriptname: tester.ps1
function main($myString, $otherVar) {
    Write-Host $myString
}
# Entry point
Write-Host "Not using splatting: " -NoNewline
main $args

Write-Host "Using splatting: " -NoNewline
main @args

Output:

PS> . .\test.ps1 -myString "Hi World" -otherVar foobar
Not using splatting: -myString Hi World -otherVar foobar
Using splatting: Hi World

Find more about the splatting operator @ here

Sign up to request clarification or add additional context in comments.

Comments

0

Based on Jeroen Mostert's comment*; the solution is below. Basically I was incorrectly trying to 'overload' or 'shadow' the built-in $arg variable. I just need to have a param with a different name like this:

 #Requires -Version 5.0
    function main($my_args) {
        write-host "From Function:" $my_args 
    }
    # Entry point
    write-host "From entry point:" $args
    main $args

> . .\tester.ps1 hello world
From entry point: hello world
From Function: hello world

Comments

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.