尝试启动数组中列出的进程,然后获取这些进程并将它们输出到文件中,然后关闭这些进程。除了让数组中列出的所有进程填充到输出文件中之外,一切都正常工作。我只拿回一个。
# 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
}