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.