Waiting for an process to quit in PowerShell

前提是你 提交于 2020-01-07 07:47:10

问题


Is there a possibility to wait for an process to quit, without it needs to running? I know there is the keyword WaitForExit, but to use this the process needs to run.

My second question is, if there is a possibility to use an else-Statement in an while loop. Tried it already, but it always said that there isnt an function called else.


回答1:


Do Until

Do {
    Sleep 5
} Until (Get-Process iexplore);

Will wait until iexplore is found

While

While (Get-Process iexplore) {
    Sleep 5
}

Will wait until iexplore is no longer running


Else after while

You cannot use an else statement after a while loop.

It needs to come after an if.




回答2:


if there is a possibility to use an else-Statement in an while loop.

If you mean something like:

while (cond) {
} else {
}

?

Then NO. (how would the content of the else block be any different to code immediately following the while block?)

Is there a possibility to wait for an process to quit,

Yes. There are different ways of doing this, depending on the nature of the target process. Is it one created by the same script? Is the same session? A service? Or just an arbitrary process?




回答3:


Does it seems to fits your needs (1st question) ? http://technet.microsoft.com/library/hh849813.aspx




回答4:


You can use wait-process cmdlet. Check link for details http://ss64.com/ps/wait-process.html

Example: wait-process -name notepad.exe


来源:https://stackoverflow.com/questions/20993797/waiting-for-an-process-to-quit-in-powershell

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