Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke-expression output on Screen and in a variable

Tags:

powershell

I am trying to store output from Invoke-expression into a variable as well as on screen. I have PS logging which automatically log everything as Write-Host in a file. Now I am using Invoke-Expression which seems to either print the output on Screen or to a variable, I need both

All that I have tried is:

$var = "C:\ER\work\Canny.exe -Init ER\ER2 Get-ip"

$val = Invoke-Expression $var

This doesn't print anything on Screen so I am unable to know if there are any issues while running. I later do a Write-Host of $val which logs it but its sometimes too late to know what happened If I use:

Invoke-Expression $var

Nothing is logged (obviously), but there is console output and if I want to see after sometime for logs what happened, I have no way of Investigating. I have also tried :

Invoke-Expression $var -OutVariable out 

OR

Invoke-Expression $var -OutVariable $out 

This is of no use here. I have also created a script block and tried with

Invoke-Command 

but again of no use I just need it to print the output on Screen as well as to a variable.

like image 337
Akshay Avatar asked May 19 '16 17:05

Akshay


People also ask

What does invoke-expression do?

Description. The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression , a string submitted at the command line is returned (echoed) unchanged. Expressions are evaluated and run in the current scope.

What is invoking in PowerShell?

Description. The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote computer, use the ComputerName parameter.

What is IEX command in PowerShell?

iex is an alias for Invoke-Expression . Here the two backticks don't make any difference, but just obfuscates the command a little. iex executes a string as an expression, even from pipe. Here Start-Process is a cmdlet that starts processes.


1 Answers

Invoke-Expression -Command $var -OutVariable out should work (variable + console output), but there's something weird going on. It works in ISE, but in a normal PowerShell console I get an empty ArrayList. If you pipe it to another command like Out-String it works (but this would return a single multi-line string).

Invoke-Expression -Command $var | Out-String -OutVariable out

Either I'm forgetting something or it might be a bug with Invoke-Expression.

A workaround would be to use Tee-Object which behaves the same as -OutVariable.

The Tee-Object cmdlet redirects output, that is, it sends the output of a command in two directions (like the letter "T"). It stores the output in a file or variable and also sends it down the pipeline. If Tee-Object is the last command in the pipeline, the command output is displayed at the prompt.

Example:

Invoke-Expression $var -OutVariable | Tee-Object -Variable out 

or (to a file):

Invoke-Expression $var -OutVariable | Tee-Object -FilePath c:\text.txt

Be aware that it overwrites the content in $out.

like image 160
Frode F. Avatar answered Nov 05 '22 17:11

Frode F.