0

I am not understanding this. If I put the param at the top of my script then run the script with command line and specify a user name, the user name is assigned to $UserName

Param([string]$UserName)

If I wrap it in a function then run it from command line:

E.g: ./Script.ps1 -UserName BLAH

It's not being assigned to $UserName

Function MainFunction
{
  Param([string]$UserName)
}
#Call Mainfunction
MainFunction;

Why is that?

5
  • It still doesn't work. I do .\Test.ps1 MainFunction -UserName test and it still doesn't store the value Commented Apr 26, 2018 at 19:26
  • 1
    What makes you think it should be? Command line parameters forced into some random function, just because the function exists, overriding the fact that you explicitly call the function without any parameters? That would be .. unusable. Commented Apr 26, 2018 at 19:26
  • Sorry I am not following you? Based on what i have read, any type of validation of the command line arguments need to be in a function. So after the Param() you use Process{Try{test}Catch{}} Commented Apr 26, 2018 at 19:29
  • That is several different ideas mixed up together. You can write code inside a script and run it from a command line. This can include parameters and parameter validation without any functions. It can (separately) include process/try/catch blocks. Separately to that, you can write code in functions, which can also include all those things, and have nothing to do with the command line. And separately to all of those things, you can 'dot-source' or 'import-module' a script, which makes the functions inside it usable directly from the command line instead of calling the script filename. Commented Apr 26, 2018 at 19:37
  • PowerShell scopes Commented Apr 26, 2018 at 21:10

2 Answers 2

2

Your code places the parameter in the scope of the function. When you run Script.ps1 -UserName BLAH this parameter would be passed in the scope of the script.

If you wanted to pass the variable down you would need two parameter blocks, one for the script and one for the function. Then pass script's UserName to the function as an argument of the function.

Param([string]$UserName)

Function MainFunction {
    Param([string]$UserName)
}

MainFunction -UserName "Chuck Norris"

Alternatively you could use the UserName from the script scope inside of the function by using the $script: to use the variable from that scope.

Param([string]$UserName)

Function MainFunction {
    Write-Output "Example use of $script:UserName"
}

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

Comments

0

Solution:

Function MainFunction
{
  Param([string]$UserName)
  Write-Host $UserName
}
#Call Mainfunction
MainFunction  -UserName $args[0]

Call From CMD:

powershell -File "Script.ps1" "Chuck Norris"

1 Comment

@user1158745 Updated the solution for CMD

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.