简体   繁体   English

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

[英]Passing array to another script with Invoke-Command

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.我试图将一个数组作为参数传递给另一个 PS 脚本。 Here's what I'm trying:这是我正在尝试的:

$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...提前致谢,伙计们,我真的很感谢你能提供的任何帮助......

Use:采用:

... -ArgumentList (,$IPArray)

Because the ArgumentList parameter expects an array you've correctly noted that you need to wrap the array in another array.因为ArgumentList参数需要一个数组,您已经正确地注意到您需要将该数组包装在另一个数组中。 However, the correct syntax in this scenario (specifying a parameter value) is to use a grouping expression () to create the nested array.但是,此场景中的正确语法(指定参数值)是使用分组表达式()来创建嵌套数组。

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+.这很愚蠢,但我再次回答,因为我尝试了所有替代方法,这是唯一有效的方法——即使使用 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)您可以将#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.向 PS 团队投下一颗炸弹,但如果我没有这样做,我的脚本将是无效的。 And I've become the "scripting guy"...so here you go.我已经成为“脚本编写者”……所以你开始吧。

This is 100% correct - may not be how this thread started but immensely useful to me. 这是100%正确的-可能不是该线程的启动方式,但对我非常有用。

Variable $Content contains 372 lines of text and should be sent to the remote computer: 变量$ Content包含372行文本,应将其发送到远程计算机:

Invoke-command -scriptblock {Param($content);$content.count} -ArgumentList @($content) -ComputerName CAT0005 调用命令-scriptblock {Param($ content); $ content.count} -ArgumentList @($ content)-ComputerName CAT0005

Result: 结果:

1 1个

Invoke-command -scriptblock {Param($content);$content.count} -ArgumentList @(,$content) -ComputerName CAT-0005 调用命令-scriptblock {Param($ content); $ content.count} -ArgumentList @(,$ content)-ComputerName CAT-0005

Result: 结果:

372 372

If you are just trying to pass one array, you can treat the $args variable itself as the array in your remote command and you get the same result as passing it as (, $IPArray) and then accessing that array in the scriptblock as $args[0].如果您只是尝试传递一个数组,则可以将 $args 变量本身视为远程命令中的数组,并且将获得与将其作为 (, $IPArray) 传递然后在脚本块中作为 $ 访问该数组的相同结果参数[0]。 I didn't test whether this works with multiple arrays or not.我没有测试这是否适用于多个数组。

That is to say,也就是说,

$MyScriptBlock = { $args | % { Do-Stuff -Thing $PSItem } }
Invoke-Command -Session $MySession -ScriptBlock $MyScriptBlock -ArgumentList $IPArray

will return the same values as将返回相同的值

$MyScriptBlock = { $args[0] | % { Do-Stuff -Thing $PSItem } }
Invoke-Command -Session $MySession -ScriptBlock $MyScriptBlock -ArgumentList (,$IPArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM