将字符串变量作为逗号分隔列表传递给函数参数 [英] Pass String Variable to Function Argument as Comma Separated List

查看:85
本文介绍了将字符串变量作为逗号分隔列表传递给函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PowerShell 函数,它通过逗号分隔列表接受单个参数的多个值,如下所示:

I've got a PowerShell function that accepts multiple values for a single argument via a comma separated list, like so:

test-function -Targets 127.0.0.1, 8.8.8.8, fake.fqdn.com -FileName "c:\results.csv"

这是参数部分:

function test-function {
    param(
        [string[]]$Targets
        [string]$FileName
    )
}

我试图将一个数组作为多个 Target 参数放入,所以我将它们放入一个变量中,如下所示:

I'm trying to put an array in as multiple Target arguments, so I've put them into a single variable like so:

foreach ($target in $arrayTargets) { $temp = $temp + ", $Target" }
$Targets = $temp -replace "^, ", ""

这将导致所需的127.0.0.1, 8.8.8.8, fake.fqdn.com"作为变量 $Targets 中的字符串.

This would result in the required "127.0.0.1, 8.8.8.8, fake.fqdn.com" as a string in the variable $Targets.

当我尝试执行引用 $Targets 变量的函数时,该函数将 $Targets 解释为一个条目,而不是多个条目.这是我尝试过的相同结果:

When I try to execute the function referencing the $Targets variable, the function interprets $Targets as if it's a single entry, not multiple. This is what I've tried with the same results:

test-function -Targets $Targets -FileName "c:\results.csv"
test-function -Targets "$Targets" -FileName "c:\results.csv"

我还尝试了几种不同的 ArgList 方法:

I've also tried an ArgList several different ways:

$ArgList = "-Targets $Targets -FileName `"c:\results.csv`""
test-function $ArgList

它不是将每个单独的数组条目作为参数,而是将整个字符串作为参数.

Instead of taking each individual array entry as an argument, it takes the entire string as the argument.

如何让这个函数接受一个数组作为多个目标?

How can I get this function to accept an array as multiple targets?

推荐答案

如果你有一个参数数组,就没有理由将它们打包成一个字符串传递给你的函数.只需传递数组.

If you have an array of arguments, there should be no reason to pack them into a string to pass to your function. Simply pass the array.

test-function -Targets $arrayTargets

要在 PowerShell 中将一组值分配给变量,只需分配一个逗号分隔的列表即可.注意要创建一个字符串数组,您应该确保将字符串放在引号中(单引号或双引号).以下所有内容都是等价的.

To assign an array of values to a variable in PowerShell, simply assign a comma-separated list. Note to make an array of strings, you should make sure to put the strings in quotes (single or double). All of the following are equivalent.

$arr = @("127.0.0.1", "8.8.8.8", "fake.fqdn.com")
$arr = ('127.0.0.1', '8.8.8.8', 'fake.fqdn.com')
$arr = "127.0.0.1", '8.8.8.8', "fake.fqdn.com"

如果您出于任何原因需要将数组转换为逗号分隔的字符串,则无需使用 foreach 循环构建它.而是使用 -join 运算符.

If you need your array to convert to a comma-separated string for any reason, there's no need to build it with a foreach loop. Instead use the -join operator.

PS> $string = $arr -join ', '
PS> $string
127.0.0.1, 8.8.8.8, fake.fqdn.com

但您可能只希望以这种形式显示数组.如上所示,最好将其保留为数组形式以传递给函数.您甚至可以使用 -split 运算符从字符串中取回数组.

But you will probably only want your array in this form to display it. As shown above, it's best to keep it in array form to pass to a function. You can even get the array back from the string using the -split operator.

PS> $string -split ', '
127.0.0.1
8.8.8.8
fake.fqdn.com

这篇关于将字符串变量作为逗号分隔列表传递给函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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