My script only works from within ISE

in my script I use Invoke-Expression to run a third part command:

Invoke-Expression $command -OutVariable out
$out > C:\temp.txt

The command takes a long time and I need to post process the output; so I save the output in $out. I then find I have to pipe it to a file before using Get-Content file -raw to read the content for post processing.
The code works when I executed the script from ISE or turn it into a Module and again run it from ISE. But if I run the same module from “Windows Powershell” or “Powershell Console” the $out contains nothing; even though I can clearly see the output from the command is printed out to the screen.
I would appreciate your help with this.
many thanks

Hi Sina,

Can you write it this way:

$out = Invoke-Expression $command -OutVariable
$out > c:\temp.txt

I’m guessing this will work if you have defined your variables.

Take care

I tried your suggestion but it fails to compile!

$(Invoke-Expression $command -OutVariable out)

hope this should take care of it.

The problem with

$(Invoke-Expression $command -OutVariable out)
is it suppresses the console output until the command has finished executing; this command takes a long time to execute and I need to provide feedback during the execution of the command - that is why I chose to use the -OutVariable option. So unfortunately, I can’t use this solution. I wonder if the issue has something to do with the length of data I need to store in $out. When I run the command in ISE the content of $out is below: is there a problem with this?

Parsing commands...
Successfully parsed 3 commands
Executing command 'ccollab addchangelist'
Creating new review.
New review created: Review #91871: "Untitled Review"
Attaching changelists to review
SCM system is configured to Team Foundation Server
Checking client configuration.
Loading information for changelist `1234`
Ensuring changelist can be uploaded
Checking that you are allowed to upload files to this review
Uploading C1234 to Collaborator Server
Loading version control data
Loading version info for $/VelocityControl/Dev/Source/AbstractionLayer/vcast/common/vx6_9_sim_buildexecute.bat
Uploading Changelist to Collaborator server
Associating C1234 with Review #91871
C1234 successfully attached to Review #91871: "Untitled Review"
Set review title to Review #91871: "C1234 - Added back timeout / delay which seems to allow tests to run on slower machines"
Executing command 'ccollab admin review edit'
Loading review ID 91871.
Set template to 'FBM249_Standard'
Set custom field 'Project ID'
Set custom field 'Project Name'.
Executing command 'ccollab browse'
Loading review ID 91871

I’ll leave it to smarter brains to explain why it works this way, but anyone can replicate it easily.

$mycommand = "ping"
Invoke-expression $mycommand -outvariable out

From the ISE you get $out filled with everything that you see passed as standard output, from a console $out is simply null.

I’m sure there is a good reason for this, which we don’t really need to know to resolve your current problem.
The functionality you really desire is covered by Tee-Object, which allows us to both capture the std out to a file AND pass it down the pipeline where it winds up getting sent out to the host.

$mycommand = "ping"
Invoke-expression $mycommand | Tee-Object -filepath c:\ToutTest.txt