2

I am trying to pass an ArrayList that contains objects into another PowerShell script to execute something further.

The error message that I am receiving is:

"Cannot process argument transformation on parameter 'al'. Cannot convert the "System.Collections.Hashable System.Collections.Hashable System.Collections.Hashable" value of type "System.String" to type "System.Collections.ArrayList""

In script1.ps1:

$al = New-Object System.Collections.ArrayList

...

$obj =  @{"var1"="apple"; "var2"="banana"; "var3"="carrot";}
$al.Add($obj)

...


foreach ($i in $al) {
    $temp = $($i.var1)
    write-host "$temp"     #outputs "apple" correctly
}

invoke-expression -Command "script2.ps1 -al '$al'"

In script2.ps1:

param ([System.Collections.ArrayList]$al)

...

foreach ($i in $al) {
    $temp = $($i.var1)
    write-host "$temp"     #error message
}
2
  • one very simple way to pass structured data between PoSh scripts is to use one of the structured data file types. JSON is the most often recommended, but the Export/Import-CliXml cmdlets handle PoSh data types very neatly. so i would export to a CliXml file & then have the other script import that file. that way all you need to pass is the full file name. [grin] Commented May 2, 2020 at 13:46
  • Please consider upvoting any answers you found helpful, or accepting an answer (via the checkbox) if it solves your problem. Commented May 3, 2020 at 15:18

1 Answer 1

2

For a reason that I'm not familiar with, Invoke-Expression is converting your ArrayList to a HashTable. If you really need an ArrayList in script2.ps1, you can make $al a global variable (see below).

Updated script1.ps1

$al = New-Object System.Collections.ArrayList
$obj = @{"var1" = "apple"; "var2" = "banana"; "var3" = "carrot"; }
$al.Add($obj)

foreach ($i in $al) {
    $temp = $($i.var1)
    write-host "$temp"     
}

$Global:al = $al

invoke-expression -Command "$PSScriptRoot\script2.ps1"

Updated script2.ps1

param()

$Global:al.GetType().FullName

foreach ($i in $Global:al) {
    $temp = $($i.var1)
    write-host "$temp"     
}
Sign up to request clarification or add additional context in comments.

3 Comments

$obj is creating a hashtable that he is adding to his ArrayList.
@Ash, $al still an still an ArrayList when used by Invoke-Expression. This can verify this by adding $al.GetType().FullName to the line before the one where script2.ps1` is invoked.
He's then converting it to a string by wrapping the variable in quotes, which is causing the problem. In reality it works. I think you still need to choose whether you need a key/value pairing such as a hashtable or dictionary, or a List/Array of objects. I'm not sure a list of hashtables is particularly helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.