Piping to More Than One Location - PowerScript

2019-08-21 16:04发布

问题:

I want to do something like this-

"Error array cleared." | Out-File $ErrorLog $InfoLog -Append

However it's not working. Is this possible without writing another line to output it to the other file?

回答1:

One way is with a short function like this:

function Out-FileMulti {
  param(
    [String[]] $filePath
  )
  process {
    $text = $_
    $filePath | foreach-object {
      $text | out-file $_ -append
    }
  }
}

Example:

"Out-FileMultiTest" | Out-FileMulti "test1.log","test2.log"

(Writes the string "Out-FileMultiTest" to both test1.log and test2.log)



回答2:

You can also use Tee-Object to accomplish the same thing. Look at example 3 on that page. Here is a quick sample that grabs the contents of the current directory and saves it to two files.

Get-ChildItem | Tee-Object -FilePath teetest.txt | Out-File teetest2.txt


回答3:

Found this snippet of code which does what I need-

"Test" | %{write-host $; out-file -filepath $ErrorLog -inputobject $ -append}