How to handle command line output in a stream as a stream

Writing SomeCommand | { ... }prints all the output SomeCommandto the code in braces. Is there a way to "lay out" one line at a time and let the code in curly brackets process it line by line, without waiting for all the output to be stored in memory?

Update

[Copied from comment]

SomeCommandis an executable file (not the powershell cmdlet) that produces some output at run time. It takes some time between the lines that it creates, and I don’t want to wait until all the lines are created to do some operation on each line

+4
source share
3 answers

script , : :

1, 2, 3 | & {
    process {
        $_
        Start-Sleep -Seconds 1
    }
}

1, 2, 3 | & {
    $input
    Start-Sleep -Seconds 1
}

, Foreach-Object ( ) , script , , script, script - , ...

+2

powershell .

, , , , , . , ( ) . .

Technet

. (, Sort-Object) , .

ForEach-Object ( %) $_, :

Get-ChildItem $pshome | ForEach-Object -Process {if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; "" }}

, , . - , powershell. System.Diagnostics.Process, Process.BeginOutputReadLine, Process.OutputDataReceived , . . .

, . Process.OutputDataReceived.

+1

Output from a text stream (old-style) executed using the PowerShell cmdlet foreach:

ping.exe www.somesite.com | %{$_}

Explanation: It %is an alias for Foreach-Object. It splits the text stream into newlines and emits lines one by one. In fact, most cmdlets that take strings as input will only work on the command line (this is actually PowerShell, which cuts the text stream, not the cmdlets).

0
source

Source: https://habr.com/ru/post/1541948/


All Articles