Does powershell have the equivalent of popen?

I need to start the process and read the output in a variable. Then, based on the return of the command, I can choose the full output or just the selected subset.

Therefore, to be clear, I want to start a text process (actually psexec) and read the output from this command (stdout, stderr, etc.) into a variable, and not directly output it to the console.

+5
source share
3 answers

You have given up some details regarding which process, but I think this article from the Powershell team blog has everything that you would like either using the pipeline or using System.Diagnostics.Process.

, , , ProcessStartInfo true RedirectStandardOutput, StandardOutput Process , , , . StandardError .

+5

, -

$output = ps

stdout, , . , $?.

, , , , - , .

+1

PowerShell Start-Process. System.Diagnostics.Process.

> $proc = Start-Process pslist -NoShellExecute

However, while it returns a Process object , it does not allow redirecting the output to execution. To do this, you can create your own process and execute it by first changing ProcessStartInfo :

> $proc = New-Object System.Diagnostics.Process
> $proc.StartInfo = New-Object System.Diagnostics.ProcessStartInfo("pslist.exe")
> $proc.StartInfo.CreateNoWindow = $true
> $proc.StartInfo.UseShellExecute = $false
> $proc.StartInfo.RedirectStandardOutput = $true
> $proc.Start()
> $proc.StandardOutput.ReadToEnd()
+1
source

All Articles