powershell - 在数组中依次调用一组函数

标签 powershell

我想创建一些函数名数组,然后根据传递给脚本的参数调用它们。这可能吗?在下面的粗略示例中,如果我将2作为参数传递给脚本,则希望按的顺序将函数的$twoFuncs列表称为

param ($whichFuncs)
Function a{ Write-Host "Executing function a" }
Function b{ Write-Host "Executing function b" }
Function c {Write-Host "Executing function c" }
$twoFuncs = @("a","b")
$threeFuncs = @("c","b","a")

if($whichFuncs -eq 2)
{
    # execute functions a + b
}

最佳答案

您正在寻找的是调用运算符&和一个foreach循环。

PS C:\Users\Frode> $twoFuncs | ForEach-Object { & $_ }
Executing function a
Executing function b

关于powershell - 在数组中依次调用一组函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21352321/

相关文章:

powershell - 使用 powershell 在 AD 中获取组织职位

powershell - 从实际变量中获取 powershell 变量名

按顺序批量重命名文件的 PowerShell 命令

python - 编码 : utf-8 doesn't seem to work

使用 CMD/C 暂停时,Powershell ISE 中断

c# - 如何在没有 Excel 或第 3 方工具的情况下将 xlsx 文件读入 .net?

reflection - 如何在Powershell中创建新的ExeConfigurationFileMap实例?

powershell - 将 Get-ChildItem 与 Get-Content 一起使用

windows - 使用命令提示符或 Powershell 找到桌面快捷方式

powershell - 文件系统对象:MoveFolder提供随机的 “Permission Denied”错误