How to start and stop processes in PowerShell?

混江龙づ霸主 提交于 2020-02-06 01:45:51

问题


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

回答1:


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.

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


来源:https://stackoverflow.com/questions/33898479/how-to-start-and-stop-processes-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!