2

So my understanding of powershell redirection is that *>&1 should redirect everything to the output stream.

Try the following from the visual studio package manager terminal on any project with Entity Framework migrations applied.

$m = (Get-Migrations *>&1)

After running, all your migrations are printed to the window, but $m is still $null.

What gives? Am I doing redirection wrong?

Edit: Seems like maybe I'm not and this should work. Here is the code for Get-Migrations

12
  • What does $m = Get-Migrations give you? Commented Dec 24, 2014 at 19:15
  • @Paul it prints out a list of the 20 or so migrations I have applied. Nothing gets stored in $m. I've seen this behavior before when using commandline tools which just write directly to the output stream rather than returning powershell objects. I seem to remember that in those cases I used redirection to resolve the issue but its not working here. Commented Dec 24, 2014 at 19:17
  • Hmm i see the cmdlet supports common parameters so you could try -outvariable Commented Dec 24, 2014 at 19:21
  • 1
    You might be able to use Start-Transcript instead. Commented Dec 24, 2014 at 19:42
  • 1
    Suppose you could try to use Trace-Command if you really feel up to it. I cant since i dont have access to that type of environment. blogs.technet.com/b/heyscriptingguy/archive/2014/12/04/… Commented Dec 24, 2014 at 19:55

1 Answer 1

2

[console]::WriteLine, which is what Get-Migrations is using, is basically the same as Write-Host, so it only prints the output and doesn't send anything through the pipeline like Write-Output does.

I found a dev named Lincoln Atkinson that has implemented a solution that uses a proxy for Write-Host at http://latkin.org/blog/2012/04/25/how-to-capture-or-redirect-write-host-output-in-powershell/

I've tested out his function and it seems to need a little work, it only worked for me when I passed the -Quiet flag. It also only works for Write-Host so updating it to work with WriteLine would be your take-home assignment from this....

Also it looks like while Start-Transcript might help (along with a generous parsing of output) if it were Write-Host based, but does not appear to catch [console]::WriteLine for whatever reason.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.