首页 > 解决方案 > 使用 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
}

标签: arrayspowershell

解决方案


  1. 您需要使用 -Append 不覆盖 process.txt
  2. 我认为您实际上不想每次都获取所有进程吗?但我可能是错的...
'' | 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
}

推荐阅读