1

I have written 5 functions as:

Function Task_1 {
‪#‎todo‬ 
}

Function Task_2 {
#todo 
}

likewise.

Now I want to call those function one by one from within a for loop as:

for($i=1; $i -le 5; $i++){
Write-Host Executing Task $i;
Task_$i;
}

When I run this, I get output & error as:

Executing Task 1

The term 'Task_$i' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

What's going wrong? Solution please!

1 Answer 1

3

Task_$i isn't the name of a function, it's just a string.

You can try using an & first:

&Task_$i

Or to be more literal:

invoke-expression Task_$i
Sign up to request clarification or add additional context in comments.

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.