I want to write a power shell script that reads from the standard input. I
try to call my script stored in test.ps1 like this:
>powershell -command .\test.ps1 < .\test.txt
or like this:
>echo foobar | powershell -command .\test.ps1
I just want to access the content of stin. But I don't know how to do this.
Reading by Read-Host only waits for keyboard input.
Can someone help me please.
Tanks and Regards
tiefseeruebe
try read-host.
sorry, I didn't read the last line. Ignore my reply.
Shay
http://scriptolog.blogspot.com
Input redirection with "<" is not supported in PowerShell. However you can
easily read the content of a file and pass it to the next command in the
pipeline like this:
get-content test.txt | test.ps1
If you want to kick off the whole process from a CMD session, you should be
able to do it like this:
powershell -command "& {get-content c:\test\test.txt | c:\test\test.ps1}"
Note that you will probably have to use absolute paths for the text file as
well as for the script (unless it is in the PATH).
Jacques
### PowerShell test script
PS C:\> get-content C:\write-input.ps1
$input | foreach {write-host $_}
### example for cmd.exe
C:\>echo bratwurst, banane | powershell -noprofile -command "$input | .
C:\write-input.ps1"
bratwurst, banane
### example for PowerShell
PS C:\> echo bratwurst, banane| powershell -noprofile -command '$input | .
C:\write-input.ps1'
bratwurst
banane
Also be aware of the different ways the comma is interpreted in each command
shell.
--
greetings
dreeschkind
Regards tiefseeruebe
> Thanks for the responses. The point seems to be the parameter -noprofile.
> With out that it does not work as expected. However I don't understand that.
Can you be a little bit more specific when you say "does not work as
expected"?
Do you get any error messages? The parameter -noprofile will just tell
PowerShell not to load your profile at startup ($profile). It should work
without the parameter as well.
--
greetings
dreeschkind
regards
tiefseeruebe