- 帖子
- 2874
- 积分
- 7021
- 技术
- 336
- 捐助
- 0
- 注册时间
- 2011-6-2
|
[转载代码] [PowerShell每日技巧]保留指向进程的句柄(20140221)
When you launch an EXE file, PowerShell will happily start it, then continue and not care about it anymore:
If you'd like to keep a handle to the process, for example to find out its process ID, or to be able to check back later how the process performs, or to kill it, use Start-Process and the –PassThru parameter. This returns a process object:
PS C:\> $process = Start-Process -FilePath notepad -PassThru
PS C:\> $process.Id
5936
PS C:\> 'You just launched process {0}.' -f $process.Id
You just launched process 5936.
PS C:\> 'CPU consumption in seconds so far: {0}.' -f $process.CPU
CPU consumption in seconds so far: 0.1092007.
PS C:\> $process.CloseMainWindow()
True
PS C:\>
|
http://powershell.com/cs/blogs/tips/archive/2014/02/21/keeping-a-handle-to-a-process.aspx |
|