Text sent to PowerShell.exe is not accepted when using [Console] :: ReadLine ()

I get data loss when calling .NET [Console]::ReadLine()to read the incoming stream in PowerShell.exe. In CMD, run:

> ping localhost | powershell -NonInteractive -NoProfile -C "do {$ line = [Console] :: ReadLine (); ('' + (Get-Date -f 'HH: mm
: ss') + $ line) | Write-Host; } while ($ line -ne $ null) "
23: 56: 45time <1ms
23:56:45
23: 56: 46time <1ms
23:56:46
23: 56: 47time <1ms
23:56:47
23:56:47

Usually, ping localhost from Vista64 looks like this, so the output above is a lot of data:

Pinging WORLNTEC02.bnysecurities.corp.local [:: 1] from :: 1 with 32 bytes of data:
Reply from :: 1: time <1ms 
Reply from :: 1: time <1ms 
Reply from :: 1: time <1ms 
Reply from :: 1: time <1ms 

Ping statistics for :: 1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

But using the same API from C # gets all the data sent to the process (excluding some differences in the new line). The code:

namespace ConOutTime {
    class Program {
        static void Main (string[] args) {
            string s;
            while ((s = Console.ReadLine ()) != null) {
                if (s.Length > 0) // don't write time for empty lines
                    Console.WriteLine("{0:HH:mm:ss} {1}", DateTime.Now, s);
            } 
        }
    }
}

Output:

00:44:30 Pinging WORLNTEC02.bnysecurities.corp.local [:: 1] from :: 1 with 32 bytes of data:
00:44:30 Reply from :: 1: time <1ms
00:44:31 Reply from :: 1: time <1ms
00:44:32 Reply from :: 1: time <1ms
00:44:33 Reply from :: 1: time <1ms
00:44:33 Ping statistics for :: 1:
00:44:33 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
00:44:33 Approximate round trip times in milli-seconds:
00:44:33 Minimum = 0ms, Maximum = 0ms, Average = 0ms

, API PowerShell #, StdIn "". PowerShell StdIn, "PowerShell.exe -Command"?

+5
3

$input PowerShell, , . , [Console]::ReadLine() - . .

C:\Users\Me> ping localhost | powershell -noninteractive -noprofile -c "$input|%{(date -f HH:mm:ss)+' '+$_}"

07:31:54
07:31:54 Pinging Sigmund [::1] with 32 bytes of data:
07:31:54 Reply from ::1: time<1ms
07:31:54 Reply from ::1: time<1ms
07:31:54 Reply from ::1: time<1ms
07:31:55 Reply from ::1: time<1ms
07:31:55
07:31:55 Ping statistics for ::1:
07:31:55     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
07:31:55 Approximate round trip times in milli-seconds:
07:31:55     Minimum = 0ms, Maximum = 0ms, Average = 0ms

C:\Users\Me>ping localhost

Pinging Sigmund [::1] with 32 bytes of data:
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms

Ping statistics for ::1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
+5

:

powershell -NonInteractive -NoProfile -C  "ping localhost | % { ('' + (Get-Date -f 'HH:mm:ss') + $_) | Write-Host; }"

, , , :)

+1

I submit my final decision, even if that was not the answer to my original question. I tried to transfer data to powershell from cmd.exe, which had a bunch of specialized tools (see Www.streambase.com). In the end, I added tools to my powershell environment.

As a result, I called an external tool from PowerShell and connected it to the extended Out-Time function ...

function Out-Time { 
param (
    [parameter(ValueFromPipeline=$true)]
    $Value, 
    [switch] $OutputEmptyLines=$false) 
process {
    if (!$OutputEmptyLines -and ($Value -eq $null -or $Value -eq '')) {
    } 
    else {
        "{0} {1}" -f @((get-date -Format "HH:mm:ss.ff"), $Value)
    }
} 
}
#test
#ping localhost | Out-Time; ping localhost | Out-Time -OutputEmpty
0
source

All Articles