krismanme
krismanme

Reputation: 63

Redirect STDOUT\STDERR to current shell from win32_process create on remote computer

I am trying to uninstall an application from within Powershell V2 on a remote machine. To call the uninstallation I am using the [WMICLASS] Accelerator, due to the fact PSRemoting is not supported in our domain ex:

@([WMICLASS]"\\$computerName\ROOT\CIMV2:win32_process").Create("msiexec.exe `/x{$GUID `/norestart `/qn")

I can successfully execute the process and receive a return value of 0

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     : 
__DYNASTY        : __PARAMETERS
__RELPATH        : 
__PROPERTY_COUNT : 2
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
ProcessId        : 9580
ReturnValue      : 0
PSComputerName   : 

The issue I have is there seems to be no obvious way to grab the output of that process and return it to my current shell

one option, although not exactly what I want is

.Create("cmd /c msiexec.exe `/x{$GUID} `/norestart `/qn > $MyLog")
Get-Content -Path \\$ComputerName\$MyLog

I would prefer a way to redirect the STDOUT\STDERR to my shell without creating a file and then reading from that file. Is this possible?

Upvotes: 0

Views: 1620

Answers (2)

krismanme
krismanme

Reputation: 63

I stumbled upon a solution and a workaround for my problem. The solution would be to create a named pipe server application, pipe output of STDOUT\STDERR to said application, than stream output from server to client.

The following link provides a method to pipe STDOUT to an application, in this case a named pipe server:

https://superuser.com/a/430475

Several methods\references to create named pipe server\clients in powershell: https://stackoverflow.com/a/24111270/4292988

https://stackoverflow.com/a/719397

How to use named pipes over network?

https://rkeithhill.wordpress.com/2014/11/01/windows-powershell-and-named-pipes/

There are other workarounds including the aforementioned PsExec. Ultimately the workaround i selected involves setting up a tcp server\client, this overcomes the downfalls of named pipes over TCP\IP in a slower network, and requires no third party software.

http://learn-powershell.net/2014/02/22/building-a-tcp-server-using-powershell/

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200213

You can't redirect STDOUT or STDERR for processes created via Win32_Process. If you need the output of the process in your console, start it via Invoke-Command:

Invoke-Command -Computer $computerName -ScriptBlock {
  msiexec.exe /x$GUID /norestart /qn
}

or use PsExec:

psexec.exe \\$computerName msiexec.exe /x$GUID /norestart /qn

Other than that your options are limited to logging to a file and reading that file, AFAICS.

BTW, msiexec.exe has a parameter for logging, so you don't really need output redirection:

 msiexec.exe /x$GUID /norestart /qn /l*v $MyLog

Upvotes: 1

Related Questions