arrays - 使用 Out-File 将进程输出到文本文件
问题描述
尝试启动数组中列出的进程,然后获取这些进程并将它们输出到文件并关闭进程。除了让数组中列出的所有进程填充到输出文件中之外,一切正常。我只拿回一个。
# Array to iterate and open the following
$array2 = "notepad", "iexplore", "mspaint"
# Iterate through array and start process
foreach($process in $array){
Start-Process $process
}
# Query process and output to text file then stop processes
foreach($process in $array){
get-process $array | Out-File process.txt
Stop-Process -Name $process
}
解决方案
- 您需要使用 -Append 不覆盖 process.txt
- 我认为您实际上不想每次都获取所有进程吗?但我可能是错的...
'' | Out-File process.txt -Force # we need to empty it from previous runs
foreach($process in $array){
get-process $process| Out-File process.txt -Append
Stop-Process -Name $process
}
推荐阅读
- python-3.x - 使用 pip 安装 Python 包失败
- flutter - RenderBox 没有布局:(StaggeredGridView Widget)
- r - 你能淡入(动画)R Markdown文档的部分吗
- python - Schrodingers JSON - 使用 json 文档时,它同时作为列表和字典出错
- python - 不能使用图像 ID 来使它们成为合格的图像链接
- python - 具有多个嵌套类别的 Pandas DataFrame 到 JSON
- reactjs - 使用 React 路由器进行历史推送堆叠
- makefile - 如何静默变量属性(分配)?
- php - Laravel 7:将数据库结果分成 3 行结果,并将每个结果放入 bootstrap4 行
- python - 在字典中查找项目忽略关键案例