如何调用带参数的函数在PowerShell中的数组? [英] How to call a function with arguments with an array in Powershell?

查看:334
本文介绍了如何调用带参数的函数在PowerShell中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个名为考虑这个剧本 arrays.ps1

 功能的CallMe
{
    参数($ ARG1,ARG2 $)
    写主机`$ ARG1是$ ARG1
    写主机`$ ARG2为$ ARG2
}的$ args =一,b的
的CallMe的$ args

输出:

  PS C:\\用户\\姆明\\文件> \\ arrays.ps1
$ ARG1是B
$ ARG2是

如果我修改它,最后一行是

的CallMe $ args.Split()

我得到的结果相同。我怎么能传递一个数组给一个函数,分裂数组元素的论点?

更新

这是更接近我在做什么:

 功能的CallMe
{
    参数($ Y,$ Z)
    写主机`$ y是$ Y
    写主机`$ z是$ Z}函数DoSomething的
{
    参数($ X)
    写主机这个功能只能使用一个ARG:$ X
}功能DoSomethingElse
{
    写主机此函数不带任何参数
}
$ funcCalls =(
    (DoSomething的,C),
    (的CallMe,(A,B)),
    (DoSomethingElse,'')
    )
的foreach($在$ funcCalls FUNC){
    写主机执行功能$($ FUNC [0])参数`$($ FUNC [1])`
    &安培; $ FUNC [0] $ FUNC [1]
}

如果我运行它,这是输出:

  PS C:\\用户\\姆明\\文件> \\ arrays.ps1
执行功能用的DoSomething参数C
此功能仅使用一个ARG:
执行功能的CallMe带有参数A B
$ Y是B
$ z是
执行功能DoSomethingElse带有参数
此函数不带任何参数


解决方案

您可以图示以数组@ 来的每个元素作为参数传递给函数。

  $阵列= @(A,B)
的CallMe @array

从更新的例子,这将是更好的存储功能ScriptBlocks而不是字符串,并使用 .Invoke()执行

  $ funcCalls =(
    ({DoSomething的@args},C),
    ({的CallMe @args},(A,B)),
    ({DoSomethingElse @args},'')
    )的foreach($在$ funcCalls FUNC){
    写主机执行功能{$($ FUNC [0])}带参数的`$($ FUNC [1])`
    $ FUNC [0] .Invoke($ FUNC [1])
}

注意参数数组中被传递给它splatted为 @args 自动变量的$ args

编辑:

如果你是从那里他们不能被存储为ScriptBlocks源读你的功能,你可以将字符串转换为ScriptBlocks与 [脚本块] ::创建()

  $ funcCalls =(
    (DoSomething的@args,C),
    ('的CallMe @args',(A,B)),
    ('DoSomethingElse @args','')
    )的foreach($在$ funcCalls FUNC){
    写主机执行功能{$($ FUNC [0])}带参数的`$($ FUNC [1])`
    $脚本= [脚本块] ::创建($ FUNC [0])
    $ script.Invoke($ FUNC [1])
}

Consider this script in a file called arrays.ps1

Function CallMe
{
    param($arg1, $arg2)
    Write-Host "`$arg1 is $arg1"
    Write-Host "`$arg2 is $arg2"    
}

$args = "a","b"
CallMe $args

Outputs:

PS C:\Users\Moomin\Documents> .\arrays.ps1
$arg1 is a b
$arg2 is

If I modify it so the last line is

CallMe $args.Split(" ")

I get the same output. How can I pass in an array to a function and split the array elements to arguments?

UPDATE

This is closer to what I'm doing:

Function CallMe
{
    param($y, $z)
    Write-Host "`$y is $y"
    Write-Host "`$z is $z"

}

Function DoSomething
{
    param($x)
    Write-Host "This function only uses one arg: $x"
}

Function DoSomethingElse
{
    Write-Host "This function does not take any arguments"   
}


$funcCalls = (
    ("DoSomething", "c"),
    ("CallMe", ("a","b")),
    ("DoSomethingElse", '')
    )


foreach ($func in $funcCalls) {
    Write-Host "Executing function $($func[0]) with arguments `"$($func[1])`""
    & $func[0] $func[1]
}

If I run it this is the output:

PS C:\Users\Moomin\Documents> .\arrays.ps1
Executing function DoSomething with arguments "c"
This function only uses one arg:
Executing function CallMe with arguments "a b"
$y is a b
$z is
Executing function DoSomethingElse with arguments ""
This function does not take any arguments

解决方案

You can 'splat' an array with @ to pass each element as an argument to a function.

$array = @('a', 'b')
CallMe @array

From your updated example, it would be better to store the functions as ScriptBlocks instead of strings and use .Invoke() to execute.

$funcCalls = (
    ({DoSomething @args}, "c"),
    ({CallMe @args}, ("a","b")),
    ({DoSomethingElse @args}, '')
    )

foreach ($func in $funcCalls) {
    Write-Host "Executing function {$($func[0])} with arguments `"$($func[1])`""
    $func[0].Invoke($func[1])
}

Notice that the array of arguments gets passed in to the automatic variable $args which is splatted as @args.

Edit:

If you are reading your functions from a source where they cannot be stored as ScriptBlocks, you can convert strings to ScriptBlocks with [scriptblock]::Create().

$funcCalls = (
    ('DoSomething @args', "c"),
    ('CallMe @args', ("a","b")),
    ('DoSomethingElse @args', '')
    )

foreach ($func in $funcCalls) {
    Write-Host "Executing function {$($func[0])} with arguments `"$($func[1])`""
    $script = [scriptblock]::Create($func[0])
    $script.Invoke($func[1])
}

这篇关于如何调用带参数的函数在PowerShell中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆