Piping to More Than One Location - PowerScript

穿精又带淫゛_ 提交于 2019-12-25 03:44:16

问题


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}



来源:https://stackoverflow.com/questions/24641824/piping-to-more-than-one-location-powerscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!