【问题标题】:Output processes to text file using Out-File使用 Out-File 将进程输出到文本文件
【发布时间】:2021-10-17 22:20:54
【问题描述】:

尝试启动数组中列出的进程,然后获取这些进程并将它们输出到文件并关闭进程。除了将数组中列出的所有进程填充到输出文件中之外,一切正常。我只拿回一个。

# 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
}

【问题讨论】:

    标签: arrays powershell


    【解决方案1】:
    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多