I want to call Invoke-Expression inside a function and print the output of the command to the console but don't return it from the function since I want to return a specific value from it.
function Foo {
$command = 'mvn clean package "-Dmaven.test.skip"'
Invoke-Expression -Command $command | Write-Host
$result = 'computed return value'
return $result
}
$fooResult = Foo
Write-Output "fooResult is: ${fooResult}"
This works, but I want to avoid using the evil Write-Host function, so I tried Write-Information instead.
But I it seems you can't pipe the output of the Invoke-Expression to Write-Information. When I simply replace Write-Host with Write-Information it behaves like the -MessageData Parameter is not provided (similar when explicitly stating -MessageData).
Is there a way to write the output of Invoke-Expression to the console, but avoid returning it from the function without using Write-Host?
You try with
OutVariable