Powershell: Synchronous Stop Process

OK. I want to stop the running process, and then continue with the script only after the process terminates.

for example: stop the media player, and then delete the media player database (if you have wmp12, you can understand the reason!) But the rest of the script starts to run before the application has time to shut down.

#stop wmp stop-process -processname wmplayer #delete dir "C:\Users\blergh\AppData\Local\Microsoft\Media Player" -filter *wmdb | foreach { del $_.fullname} 

And I get:

 Remove-Item : Cannot remove item C:\Users\blergh\AppData\Local\Microsoft\Media Player\CurrentDatabase_372.wmdb: The proces s cannot access the file 'C:\Users\blergh\AppData\Local\Microsoft\Media Player\CurrentDatabase_372.wmdb' because it is bei ng used by another process. 

Can I make it work synchronously?

Thanks, P

+2
source share
3 answers

What about:

 $wmplayer = get-process wmplayer stop-process $wmplayer.id wait-process $wmplayer.id -erroraction:silentlycontinue 

Please note that an error message has been added so that you do not receive an error when the process was quickly killed,

or as a more convenient single-line layer:

 stop-process -processname wmplayer -passthru| wait-process 
+5
source

Not tested, but you can try:

 stop-process -processname wmplayer get-process | where-object {$_.HasExited} 
0
source

I would say something like this:

 calc calc calc Stop-Process -Name calc | Foreach { While ($_ -and !$_.HasExited) { Start-Sleep -milli 300 } } 
0
source

Source: https://habr.com/ru/post/950301/


All Articles