Fred
Fred

Reputation: 4076

I can't seem to capture warning output from a remote Invoke-command call

The following SO question seems to think it's possible: Powershell logging from invoke-command

The top answer (not the accepted answer), mentions the documentation states that Inovke-Command returns everything, which indeed it does say that, emphasis mine.

https://technet.microsoft.com/en-us/library/hh849719.aspx

The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors.

Note that $PSVersionTable.PSVersion outputs 4.0 (Powershell ISE).

Here is the code in question

$out = Invoke-Command -Session $sess {
  Write-Warning 'w1'
}


'--'
$out
'--'

And the output

WARNING: w1
--
--

What I was hoping for was:

--
w1
--

Meaning, I want to be able to capture the output from the warning stream.

Any explanations? How do I do this?

Upvotes: 2

Views: 583

Answers (1)

Adrian Rodriguez
Adrian Rodriguez

Reputation: 192

Use the WarningAction and WarningVariable parameters of Invoke-Command.

Upvotes: 0

Related Questions