ArgumentList parameter in Invoke-Command don't send all array [duplicate]
Asked Answered
H

1

12

First short code, then question

$session = New-PSSession -ComputerName someServer

$servicesList = "Service1", "Service2", "Service3"

Invoke-Command -ScriptBlock {
    Param ($newServicesList)

    Write-Host $newServicesList

} -ArgumentList $servicesList -Session $session

Remove-PSSession  $session

The question is why Write-Host in Invoke-Command block give only this output ?

Service1

Thanks for any answers

Halden answered 11/9, 2013 at 14:28 Comment(3)
possible duplicate of Passing array to another script with Invoke-Command Note: I tested this solution against your script and it fixed the issue.Compliment
@Hyper Anthony - Should I delete the answer if it is duplicate?Shouldst
@Mitul: meta.stackexchange.com/questions/132600/… (I wouldn't worry about it, there's nothing wrong with the answer.)Compliment
S
19

You solution is to pass it like (,$servicesList)

$session = New-PSSession -ComputerName .

$servicesList = "Service1", "Service2", "Service3"

Invoke-Command -ScriptBlock {
    Param ([string[]]$newServicesList)

    Write-Host $newServicesList

} -ArgumentList (,$servicesList) -Session $session

Remove-PSSession  $session

possible explanation from this SO answer.

Shouldst answered 11/9, 2013 at 14:56 Comment(1)
Or -args (,('Service1', 'Service2', 'Service3')). That sucks.Stereotype

© 2022 - 2024 — McMap. All rights reserved.