Piping to More Than One Location - PowerScript

2019-08-21 16:05发布

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?

3条回答
女痞
2楼-- · 2019-08-21 16:35

Found this snippet of code which does what I need-

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

查看更多
Rolldiameter
3楼-- · 2019-08-21 16:39

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)

查看更多
不美不萌又怎样
4楼-- · 2019-08-21 16:50

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
查看更多
登录 后发表回答