TransWikia.com

Running 'Start-Process' Remotely: How to Display Standard Output?

Super User Asked by Fitzroy on January 7, 2021

I have a PowerShell script for installing software on remote computers.

To date I have been successfully using the following code:

$prog = "ping"
$arg = "localhost"
$computername = "MACHINE01"
invoke-command -computername $computername {param($p,$a)& $p $a} -ArgumentList $prog,$arg

I now need to install an MSI, Eg. ‘msiexec /i c:file.msi /passive’.

I cannot get MSIEXEC to treat everything after ‘MSIEXEC’ as parameters.
Instead, PowerShell just tries to execute it as one big command.
I have had tried numerous things mostly involving the placement of literal quotes but cannot get this to work.

I have now abandoned the call operator (&) in favour of ‘Start-Process’ which has an ‘-ArgumentList’ parameter.
The MSI now executes correctly. Great!

invoke-command -computername $computername {param($p,$a) start-process $p -argumentlist $a -nonewwindow -wait -redirectstandardoutput c:output.txt; get-content c:output.txt} -ArgumentList $prog,$arg

The problem with ‘Start-Process’ is that it does not produce any console output when run remotely using ‘Invoke-Command’.
I have had to resort to redirecting the output to a file and then reading the file. Is there a better way?

One Answer

I would try piping it to the tee-object cmdlet, then saving your file there if needbe (I don't know if sending the file output to $null would work, too bad this isn't linux and we could send it to /dev/null, but I digress)

This is the tee-object cmdlet http://technet.microsoft.com/en-us/library/ee177014.aspx

invoke-command -computername $computername {param($p,$a) start-process $p -argumentlist $a -nonewwindow -wait | tee-object -file c:output.txt} -ArgumentList $prog,$arg

that's untested code, but that's generally what you would want.

invoke-command -computername $computername {param($p,$a) start-process $p -argumentlist $a -nonewwindow -wait | tee-object -file $null} -ArgumentList $prog,$arg

might work as well.

Answered by MDMoore313 on January 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP