Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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

like image 651
Sameer Avatar asked Jul 10 '13 18:07

Sameer


People also ask

How to pass arguments in invoke-command in PowerShell?

How to pass arguments in Invoke-Command in PowerShell? To pass the argument in the Invoke-command, you need to use - ArgumentList parameter. For example, we need to get the notepad process information on the remote server.

What is the use of invoke command in PowerShell?

Introduction to PowerShell Invoke-Command. Invoke-Command is used to run any command on a local or remote computer and return its output. It allows us to write a script or a command block and invoke that script or command block. It will return the result of command execution. This command can be run on local or remote.

How do I pass arguments from one bash script to another?

If I want to use these in another script, how do I pass these arguments to the other script, then run the script in my current Bash script. You would pass them pretty much the same as you would pass arguments in any other way: sed -i 's/ = /=/' "$file" source "$file" /path/to/another/script.sh "$variable1" "$variable2"

How do you pass an array as an argument in PowerShell?

So when you give -arg $myarr, it is as though you are passing the elements of the array as the arguments. So you have to force powershell to treat it as a single argument which is an array. How would you pass the array and another variable? -arg (,$myarr, $singleValue). For the example, $singleValue = "x"


2 Answers

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.

like image 128
Keith Hill Avatar answered Sep 16 '22 20:09

Keith Hill


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).

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)

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.

like image 36
areyling Avatar answered Sep 19 '22 20:09

areyling