How can I read data from standard input?

Welcome Forums General PowerShell Q&A How can I read data from standard input?

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
2 years, 9 months ago.

  • Author
    Posts
  • #34300

    Participant
    Points: 0
    Rank: Member

    I will appreciate help with the following question.

    In PowerShell, how can I read data from the standard output (Data that is generated by an automated process, and not by a user)?

    Consider the following scenario:
    A. A Java App launches a VBScript program (by running "cscript.exe")
    B. While the VBScript is running, the Java app writes data into the standard output.
    C. The VBScript reads this information from the standard input (using Wscript.StdIn.ReadLine) .

    I want to convert the VBScript program to PowerShell. I tried to use "Read-Host" to read data from the standard input, but it waits endlessly (probably waiting for user to press "Enter").

    So how can I do something similiar to VBScript "Wscript.StdIn.ReadLine" in PowerShell?

    Best Regards,

    Tal

  • #34306

    Keymaster
    Points: 8
    Rank: Member

    Just set a variable.

    $a = ping 192.168.100.100

    $a will contain whatever text ping.exe produced. Read-Host isn't for reading from stdout. It's for reading, as the name implies, from the console host. In my example here, $a will be a collection of strings. One string for each line of output produced by ping.exe. You could then enumerate those lines to parse them.

The topic ‘How can I read data from standard input?’ is closed to new replies.