I'm trying to pipe the output of my script to a separate method and trying to separate the error stream there. Here is the pipeline.ps1
Function MyLogs{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$True)]
[String[]] $Log
)
Begin {
Write-Verbose "initialize stuff"
# code to initialize stuff
}
Process {
Write-Output " $Log"
}
End {
# code to clean stuff
}
}
#--- pipe the script output to it
& .\MyScript.ps1 | MyLogs
And here is MyScript.ps1
Write-Output "********** This is a normal text message ************* `n`r "
# this would create a divide by zero error
1/0
Write-Warning "example warning"
Write-Output "after the errors"
The call & .\MyScript.ps1 | MyLogs would not pipe the error stream to MyLogs() however, errors would be displayed in the console:
PS D:\Learn\powershell> .\pipelines.ps1
VERBOSE: initialize stuff
********** This is a normal text message *************
Attempted to divide by zero.
At D:\Learn\powershell\MyScript.ps1:3 char:1
+ 1/0
+ ~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
WARNING: example warning
after the errors
And if I do like & .\MyScript.ps1 *>&2 | MyLogs, the error stream would be read as a normal text and I can't figure-out how to separate the errors from normal text.