Question:
I’m trying to have a script with both executable code and a function, like the following:
1 2 3 4 5 6 7 8 9 10 |
function CopyFiles { Param( ... ) ... } // Parameter for the script param ( ... ) // Executable code |
However, I run into the following error: “The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property”
When I list my function at the end of the file, it says that the function name is undefined. How do I call a powershell function from executable code within the same script?
Answer:
The correct order is:
1.Script parameters
1 2 3 |
# Parameter for the script param([string]$foo) |
2.Function definitons
1 2 3 4 5 |
function CopyFiles { Param([string]$bar) ... } |
3.Script code
1 2 3 |
# Executable code CopyFiles $foo $bar |
Why would you want it any other way?