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

Zabaa
  • 327
  • 1
  • 4
  • 16
  • 2
    possible duplicate of [Passing array to another script with Invoke-Command](http://stackoverflow.com/questions/17577705/passing-array-to-another-script-with-invoke-command) Note: I tested this solution against your script and it fixed the issue. – Anthony Neace Sep 11 '13 at 14:34
  • @Hyper Anthony - Should I delete the answer if it is duplicate? – Mitul Sep 11 '13 at 14:57
  • 1
    @Mitul: http://meta.stackexchange.com/questions/132600/should-i-delete-my-answer-to-a-question-thats-marked-as-a-duplicate (I wouldn't worry about it, there's nothing wrong with the answer.) – Anthony Neace Sep 11 '13 at 14:59

1 Answers1

18

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.

Community
  • 1
  • 1
Mitul
  • 9,734
  • 4
  • 43
  • 60