tags:

views:

31

answers:

1

Someone knows for which strange reason Powershell doesn't show de 'tee'd' variable in the following snippet?

# a.txt contains any text
cat a.txt | tee -variable foovar | % { 'value of foovar: ' + $foovar }

In practice, I'd like to do, in only one line, a search for some text and then, based on the results, process the text, for example send a mail with it:

get-content [somefile] | select-string [somePattern] | tee [var] | ..[more processing] | .\sendmail.ps1 [var]
A: 

It looks like tee works in your script, but not the way you are expecting. It does indeed send the whole array to $foovar, but you can't use it until the next line. It looks like that's the nature of tee. You can see that it works this way by running your script two times in a row. On the second run $foovar has the entire contents of a.txt.

It looks like you want each line of the file set to foovar, not the whole array (unless I'm mistaken). If that's the case when you send everything to a foreach loop you are setting it to the special $_ var anyway... i.e.

cat a.txt |%{'value of line: ' $_}

The above will work with select-string too.

Toenuff