MortenB
MortenB

Reputation: 3529

powershell stdout/stderr during executing (.foreach{})

How can I get stdout/stderr directly when executing a powershell script calling commands?

example:

$choco="C:\ProgramData\chocolatey\bin\choco.exe"
$chocoPackages = @('googlechrome','firefox','selenium-chrome-driver','selenium-gecko-driver')
$chocoPackages.foreach{ & "$choco" install $_ --yes --force 2>&1 }

But I do not get any output until the entire array is finished/failed.

Upvotes: 0

Views: 243

Answers (2)

js2010
js2010

Reputation: 27443

It looks like that's how .foreach{} works. Try foreach(){} or foreach-object{}

(1..5).foreach{$_;sleep 1}

Upvotes: 2

Alex_P
Alex_P

Reputation: 2952

Have you considered Start-Process?

Start-Process -FilePath "C:\ProgramData\chocolatey\bin\choco.exe" `
              -ArgumentList "install", $_, "--yes --force" `
              -RedirectStandardError .\InstallError.log -RedirectStandardOutput .\InstallOutput.log

Upvotes: 1

Related Questions