gpt4 book ai didi

powershell - 从for循环中调用Powershell函数

转载 作者:行者123 更新时间:2023-12-03 01:13:39 24 4
gpt4 key购买 nike

我已经写了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com