我想创建一些函数名称数组,然后根据传递给我的脚本的参数调用它们。这可能吗?在下面的粗略示例中,如果我将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
}
答案 0 :(得分:5)
您正在寻找的是调用运算符&
和foreach循环。
PS C:\Users\Frode> $twoFuncs | ForEach-Object { & $_ }
Executing function a
Executing function b
答案 1 :(得分:1)
如何使用开关?
param ($whichFuncs)
Function a{ Write-Host "Executing function a" }
Function b{ Write-Host "Executing function b" }
Function c {Write-Host "Executing function c" }
switch ($whichFuncs)
{
2 {a;b}
3 {c;b;a}
}