我正在尝试执行CMD命令并将命令的结果流式传输到流式编写器。这样做的原因是我试图筛选的日志文件各自高达1GB,并且将结果存储到变量中可以轻松地使用几个内存。由于性能优美,我还想输出StreamWriter。
以下是我的剧本摘录。
$StreamWriter = [System.IO.StreamWriter] "test.txt"
$SearchString = "2016_06_*"
$SearchLogs = "2016_*.log"
"CMD /C gunzip.exe -c $SearchLogs |grep -i `"$SearchString`"" | $StreamWriter.WriteLine($_)
$StreamWriter.Flush()
$StreamWriter.Close()
答案 0 :(得分:0)
-like
运算符。ForEach-Object
语句中。$StreamWriter = [IO.StreamWriter]'test.txt'
$SearchString = "2016_06_*"
$SearchLogs = "2016_*.log"
gunzip.exe -c $SearchLogs |
Where-Object { $_ -like $SearchString } |
ForEach-Object { $StreamWriter.WriteLine($_) }
$StreamWriter.Flush()
$StreamWriter.Close()