0

I'm trying to use Invoke-Command to run a command in PowerShell remotely on a number of machines, and capture their output from it, but I'm not getting any output from it. I suspect it's from how I'm using Start-Process, but I'm not sure.

$RunCommand = {
    Start-Process "$env:ProgramFiles\Some Program\someprogram.exe" -ArgumentList "-SignatureUpdate"
}

$comp_list = @(Get-Content "c:\temp\comp_list.txt")
$cred = Get-Credential

$jobs = Invoke-Command -Credential $cred -Computer $comp_list -ScriptBlock $RunCommand -AsJob

Wait-Job $jobs
$r = Receive-Job $jobs
$r | % { $_ > c:\temp\$($_.PScomputerName).output }

Is there a better way to invoke a command using environment variables like that?

3
  • This may be a typo but shouldn't it be Invoke-Command -ComputerName not just -computer Commented Mar 24, 2015 at 1:47
  • -Computer is an alias for -ComputerName, so either will work just fine. Commented Mar 24, 2015 at 1:53
  • ahh, should have figured. Have you tried using $ExecutionContext.InvokeCommand.NewScriptBlock() to create the script block. I've used this with Invoke-Ssh command Commented Mar 24, 2015 at 2:00

1 Answer 1

1

I've found that Start-Process is probably not the best way to capture the output from a binary unless the binary itself passes on .NET objects. What I did find that works better for this is the call operator:

$RunCommand = {
    $exe = "$env:ProgramFiles\Some Program\someprogram.exe"
    & $exe -SignatureUpdate
}

I was still not getting the output I was expecting. More specifically, I was only getting the last line of output from the command instead of the entire thing. Eventually it dawned on me that all the previous lines were being overwritten, and so I changed the last line to this:

$r | % { $_ >> c:\temp\$($_.PScomputerName).output }

Note: I changed > to >> for appending to the file.

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

1 Comment

To note, this has the drawback that rerunning the script results in it appending to the end of that same file instead of creating a new one.

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.