使用Invoke-Command将数组传递给另一个脚本

时间:2022-06-14 21:48:20

I'm stuck trying to figure this out. I've seen the other articles and have tried everything, but I'm not getting anywhere. I am trying to pass an array as an argument to another PS script. Here's what I'm trying:

我一直试图解决这个问题。我已经看过其他文章,并尝试了一切,但我没有得到任何结果。我试图将一个数组作为参数传递给另一个PS脚本。这是我正在尝试的:

$IPArray = Get-Content C:\ListofIps.txt
Invoke-Command -Computername $server -Credential $cred -FilePath "C:\script.ps1" -ArgumentList (,$IPArray)

The 5 values in $IPArray aren't being passed to the script that I call.

$ IPArray中的5个值未传递给我调用的脚本。

Thanks in advance, guys, I really appreciate any help you can give...

在此先感谢,伙计们,我真的很感激您能给予的任何帮助......

2 个解决方案

#1


11  

Use:

... -ArgumentList (,$IPArray)

Because the ArgumentList parameter expects an array you've correctly noted that you need to wrap the array in another array. However, the correct syntax in this scenario (specifying a parameter value) is to use a grouping expression () to create the nested array.

因为ArgumentList参数需要一个数组,所以你已经正确地指出你需要将数组包装在另一个数组中。但是,此方案中的正确语法(指定参数值)是使用分组表达式()来创建嵌套数组。

#2


0  

This is an old question, but I'm going to re-iterate @keith-hill on the answer--add a comma at the beginning of the array declaration (even when including an existing array).

这是一个老问题,但我将在答案上重新迭代@keith-hill - 在数组声明的开头添加一个逗号(即使包含现有数组)。

It's stupid, but I'm answering again because I tried all alternatives and that's the only thing that works--even with PowerShell 3.0+. You can use #require for anything from at least v3.0+, but nothing will work unless you do what @keith-hill suggests--even though it's an array, and the parameter is an array, PS sucks in this case (and I love PS)...do what he said (posting didn't work, so sorry but working answers are better): \ ... -ArgumentList (,$IPArray)

这是愚蠢的,但我再次回答,因为我尝试了所有替代品,这是唯一有效的 - 即使使用PowerShell 3.0+。你可以使用#require来获得至少v3.0 +的任何东西,但是除非你做@ keith-hill建议的东西,否则什么都不会起作用 - 即使它是一个数组,参数是一个数组,PS在这种情况下很糟糕(和我喜欢PS)...做他说的(发帖不起作用,很抱歉,但工作答案更好):\ ... -ArgumentList(,$ IPArray)

It makes no sense, but that works. Hands down to the PS team for dropping the bomb on this one, but if I hadn't done that my script would be null and void. And I've become the "scripting guy"...so here you go.

这没有任何意义,但是有效。向PS团队请求放弃炸弹,但如果我没有这样做,我的剧本将无效。而且我已成为“脚本家伙”......所以你走了。

#1


11  

Use:

... -ArgumentList (,$IPArray)

Because the ArgumentList parameter expects an array you've correctly noted that you need to wrap the array in another array. However, the correct syntax in this scenario (specifying a parameter value) is to use a grouping expression () to create the nested array.

因为ArgumentList参数需要一个数组,所以你已经正确地指出你需要将数组包装在另一个数组中。但是,此方案中的正确语法(指定参数值)是使用分组表达式()来创建嵌套数组。

#2


0  

This is an old question, but I'm going to re-iterate @keith-hill on the answer--add a comma at the beginning of the array declaration (even when including an existing array).

这是一个老问题,但我将在答案上重新迭代@keith-hill - 在数组声明的开头添加一个逗号(即使包含现有数组)。

It's stupid, but I'm answering again because I tried all alternatives and that's the only thing that works--even with PowerShell 3.0+. You can use #require for anything from at least v3.0+, but nothing will work unless you do what @keith-hill suggests--even though it's an array, and the parameter is an array, PS sucks in this case (and I love PS)...do what he said (posting didn't work, so sorry but working answers are better): \ ... -ArgumentList (,$IPArray)

这是愚蠢的,但我再次回答,因为我尝试了所有替代品,这是唯一有效的 - 即使使用PowerShell 3.0+。你可以使用#require来获得至少v3.0 +的任何东西,但是除非你做@ keith-hill建议的东西,否则什么都不会起作用 - 即使它是一个数组,参数是一个数组,PS在这种情况下很糟糕(和我喜欢PS)...做他说的(发帖不起作用,很抱歉,但工作答案更好):\ ... -ArgumentList(,$ IPArray)

It makes no sense, but that works. Hands down to the PS team for dropping the bomb on this one, but if I hadn't done that my script would be null and void. And I've become the "scripting guy"...so here you go.

这没有任何意义,但是有效。向PS团队请求放弃炸弹,但如果我没有这样做,我的剧本将无效。而且我已成为“脚本家伙”......所以你走了。