Invoke-Command -Args doesn't pass $myArray correctly

by May 25, 2011

I'm looking the best way to pass an ascii file to $myArray, and then pass $myArray to a remote script using Invoke-Command -Args.

Part of my problem is the content of the strings, part is the different types of $myArray, part is the limited environment of a remote session.

First off, I'm finding that Invoke-Command using the -file option can only pass an argument through the -Args list? I got the -Args to work, but had no luck with adding a param() statement at the top of the remote script. can a param() statement be used in place of -Args? I found -Args and param() to be interchangeable except for the Invoke-Command… is this correct? It would save me lots of time if I know this to start.

The results that I'm getting in the remote environment show the array delineation is not there. Using the GC or Get-Content works just fine with $myArray in every way until I try to work remotely and pass it through Invoke-Command -Args.

I can pass a simple hash table through the Invoke-Command -Args and it works. I can pass a simple array through the Invoke-Command and it works, showing that the Invoke-Command -Args can pass arrays, hash tables and strings… it's just $myArray that does not work.

I am reading the array in with a Get-Content call, so I'm assuming it has line feeds and carriage returns, and the array delineation is the end of the line, not a hard comma or semicolon.

Here is $myArray… The syntax of the ascii.txt makes for another difficultly in choosing escape characters and delineators, thankfully, it does not have commas or semicolons.

contents of ascii.txt

1" = 1'
1" = 100'
1" = 200'
1" = 300'
1" = 400'
1" = 500'
1" = 800'
1" = 1000'
1" = 2000'
3/8" = 1'-0"
1/2" = 1'-0"
3/4" = 1'-0"
1" = 1'-0"
1 1/2" = 1'-0"

Actually, I'm considering converting the ascii.txt file to hex just to escape all these escape characters. The foot mark at the end jams most array commands, the " jams it again, and / and = and spaces and so on…

So my big question here is how do I create a strongly typed array to get this ascii.txt contents through the Invoke-Command -Args in a way to show the values correctly in a remote session.

Currently, in the remote session, I get the first few characters like this:

1
"

=

1

and then it just stops…

So it appears that the array delineation is not there to keep these strings together… something goes wrong with $myArray in the Invoke-Command -Args list.

Can you help with my $myArray?

Could hex coding and decoding be worth looking into in this situation?

I'm currently tring to make the above ascii.txt into one long string, seperated by commas, and then feed that into an $array variable and then see if it's different in the Invoke-Command -Args

Any help would be greatly appreciated.

Rich Oswald
roswald72@hotmail.com