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

标签 powershell powershell-2.0

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

相关文章:

Windows 任务计划程序 - 将文件从 URL 复制到桌面

windows - 如何使用 Get-Service 命令获取 Windows 服务的物理路径

powershell - 使用 MSBuild 和 PSake 确定解决方案是否编译

powershell - PowerShell 是否要求脚本 block 的左花括号在同一行上?

azure - 使用筛选器获取 Azure 资源

PowerShell:检测脚本函数中的错误

bash - bash 中的命令行解析转换为 powershell

powershell - 为什么我不能让 Jenkins Powershell 插件工作?

powershell - 通过Powershell 2.0发送电子邮件

powershell - 将文件从一个源并行复制到多个目标