如何在PowerShell中启动和停止进程? [英] How to start and stop processes in PowerShell?
问题描述
这在早于3的PowerShell中应该可以正常工作.我需要运行两个进程:wuapp.exe和desk.cpl(带有ScreenSaver的选项卡).我遇到的问题是,一旦启动wuapp.exe,进程名称就会在任务管理器中显示为explorer.exe-当前代码适用于wuapp.exe,但并非在每台计算机上都有效.另一件事是,当我杀死rundll32时,其他应用程序也将关闭.你有什么建议?
This should work fine in PowerShell older than 3. I need to run two processes: wuapp.exe and desk.cpl with ScreenSaver's tab. The problem I have is that once I start wuapp.exe the process name shows up in Task Manager as explorer.exe - the current code works for wuapp.exe but not on every machine. The other thing is when I kill rundll32 other apps will close also. What do you suggest?
$wshell = New-Object -ComObject Wscript.Shell
Start-Process wuapp.exe
(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}
Start-Process rundll32 desk.cpl,InstallScreenSaver
Stop-Process -ProcessName rundll32* -Force
推荐答案
我不完全确定wuapp.exe
显示为explorer.exe
的意思,但是跟踪rundll32
的过程非常简单.
I'm not entirely sure what you mean by wuapp.exe
showing up as explorer.exe
, but keeping track of the rundll32
process is fairly straightforward.
将Start-Process
与PassThru
参数一起使用,以使其返回其创建的过程:
Use Start-Process
with the PassThru
parameter to have it return the process it creates:
$ScreenSaverCpl = Start-Process rundll32 desk.cpl,InstallScreenSaver -PassThru
# Call Stop-Process when no longer needed:
Stop-Process $ScreenSaverCpl
这篇关于如何在PowerShell中启动和停止进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!