1

I'd like to connect to a remote host, run 2 commands and return the seperate responses. However I'd like to do this as part of one scriptblock. I've done this before with one command but no joy with two. For example having

gc "C:\test.txt"

and

get-webservice | ? {$_.Name -eq "foo"}

combined into a scriptblock and passing that scriptblock to Invoke-Command and extracting the individual responses from that call.

user1054637
  • 695
  • 11
  • 28

3 Answers3

3

One option is to load your results into a hash table, the return that.

$Scriptblock = 
{
  $response = @{}
  $response.dir = gc "C:\test.txt"
  $response.service = get-webservice | ? {$_.Name -eq "w32time"}
  $response
}

$result = &$Scriptblock

This eliminates any ambiguity in the results of any of the commands returns a null.

mjolinor
  • 66,130
  • 7
  • 114
  • 135
1

Not entirely sure I understand your problem, have you tried like this:

$Workload = {
    $TestText = Get-Content "C:\test.txt"
    $WebServices = Get-WebService | ? {$_.Name -eq "foo"}

    $TestText,$WebServices
}

$FirstJob,$SecondJob = Invoke-Command -Session $remoteSession -ScriptBlock $Workload
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • I gave it a plus one point, but wanted to add this observation: the output from $WebServices shows up for $FirstJob, and versa-visa, I would have expected the reverse. – glenn garson Oct 11 '16 at 16:16
0
function scriptBlockContent ($myFile)
{
    $TestText = Get-Content $myFile
    $WebServices = Get-WebService | ? {$_.Name -eq "foo"}

    $TestText,$WebServices
}

$FirstJob,$SecondJob = Invoke-Command -Session $remoteSession -ScriptBlock ${function:scriptBlockContent} -ArgumentList $myFile
Andrew Semenov
  • 151
  • 1
  • 3