我已经写了5个函数:
Function Task_1 {
#todo
}
Function Task_2 {
#todo
}
同样。
现在,我想在for循环中一一调用这些函数:
for($i=1; $i -le 5; $i++){
Write-Host Executing Task $i;
Task_$i;
}
运行此命令时,输出为:
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.
怎么了请解决!
最佳答案
Task_ $ i不是函数的名称,它只是一个字符串。
您可以先使用&:
&Task_$i
或更确切地说:
invoke-expression Task_$i
关于powershell - 从for循环中调用Powershell函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34471313/