我知道我使用以下方法写入文件:
$stream = [System.IO.StreamWriter] "file.txt"
因为我需要使用流,所以我真的很想知道是否可以用某种东西替换“file.txt”,以便将写入流的内容打印到控制台(写入主机)或写入进度?
提前致谢
最佳答案
我会考虑编写一个函数来执行这两个操作。这是非常通用的,但会从管道获取您想要的输入,然后写入流并将输出写入控制台。
# Generic function name modeled after Tee-Object, which outputs to console and file
Function Tee-Object1 {
Param (
[parameter(ValueFromPipeline=$True)]
$InputObject
)
Process {
# Write to stream
[void]$Script:Stream.Write($_)
# Write to console
$_
}
}
$Script:stream = [System.IO.StreamWriter] "file.txt"
# Whatever your data is will be piped into the function
$data | Tee-Object1
关于ios - PowerShell [System.IO.StreamWriter] 写入控制台(Write-Host)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24610858/