is there an easy way in powershell to output a string to variable and console at the same time?
i want to capture the output of my script to a variable so i can analyze it in the end of the script, save it to a log file and also email to an operator.
my intent is to have a variable $output and add any output strings to it and also output to console immediately something like
$output="Process started"
$output=+"Doing step 1"
"Doing step 1"
$output=+"Doing step 2"
"Doing step 2"
so in the end I can save $output to a log file, email it and parse it.
I played with tee-object that might work for that purpose but unfortunately it would rewrite my $output variable instead of appending a string to it.
UPDATE
This is the final solution I decided to go with – thanks to manojlds!
$script:output = ""
filter mylog {
$script:output+= $_+"`n"
return $_
}
"doing step {0}" -f 1 | mylog
"doing step {0}" -f 2 | mylog
"doing step {0}" -f 3 | mylog
#in the end of the script
$script:output
There are so many ways to get your end goal:
In your script just have something like this:
Then run the script as
Note that the output is available to
Tee-Objectand hence the console as and when it occurs. You don’t get the output only after the entire script runs. This is how pipeline works in Powershell. Objects are passed along downstream as and when they occur. Insert asleep 10in between as steps and see that the output comes as soon as it is available.Also, you don’t necessarily have to have another script ( the launcher.ps1 ) you are talking about. You can use functions, scriptblock etc. within your script.
Some other ways:
You can create a filter:
There are probably many more ways of doing this.