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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.