我的目标是运行多个进程,并保存它们的Processname
和Id
供以后使用。这是我的代码
[System.Collections.ArrayList]$startedProcesses = @()
$processStatus = Start-Process -FilePath notepad.exe -passthru
Start-Sleep 1
$startedProcesses.add($processStatus)
$processStatus = Start-Process -FilePath calc.exe -passthru
Start-Sleep 1
$startedProcesses.add($processStatus)
echo $startedProcesses
此脚本的输出为:
PS C:\Users\wakatana\Desktop\> .\so_question0.ps1
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI Processname
------- ------ ----- ----- ------ -- -- -----------
235 15 3408 14112 0.08 24812 2 notepad
0 0 0 0.13 21460
我也尝试替换[System.Collections.ArrayList]$startedProcesses = @()
以$startedProcesses = New-Object System.Collections.Generic.List[System.ComponentModel.Component]
结束,但结果还是一样。
问题:为什么我在calc
下没有Processname
?如果删除Start-Sleep 1
,则在calc
下得到Processname
。这是为什么?这是启动应用程序的正确方法,还是我做错了什么?我的操作系统:Windows 10 Home