Why are teed arent variables visible in later script blocks?

Does anyone know for some strange reason Powershell doesn't display the '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 would like to search for some text in only one line, and then, based on the results, process the text, for example, send him a message:

get-content [somefile] | select-string [somePattern] | tee [var] | ..[more processing] | .\sendmail.ps1 [var] 
+3
source share
3 answers

Since all the output that was passed in Tee-Objectmust be accessible in a variable, the cmdlet caches its input and writes it to the variable at the end.

But you can also do the following:

$var = gc a.txt
$var | your_processing_stuff
+2
source

You can also use -OutVariable

cat c:\dev\NamingConventions.txt -OutVariable test1 | % { write-host 'value: ' $_ }
$test1
cat c:\dev\NamingConventions.txt | select-string -patt panel -OutVariable test2
$test2
+1
source

, tee script, , . $foovar, . , . , , script . $foovar a.txt.

, , ​​ foovar, ( ). , foreach, $_ var anyway... i.e.

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

select-string.

0

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


All Articles