如何启动进程,暂停2小时,然后在Powershell中终止进程
如何启动进程,暂停2小时,然后在Powershell中终止进程。我可以让它启动进程并终止进程,但Start Sleep命令在我的脚本中似乎不起作用。我想这很简单。不确定我是否错过了什么,或者这是否可以睡两个小时如何启动进程,暂停2小时,然后在Powershell中终止进程,powershell,Powershell,如何启动进程,暂停2小时,然后在Powershell中终止进程。我可以让它启动进程并终止进程,但Start Sleep命令在我的脚本中似乎不起作用。我想这很简单。不确定我是否错过了什么,或者这是否可以睡两个小时 if((Get-Process -Name test -ErrorAction SilentlyContinue) -eq $null){ ."C:\Program Files (x86)\test.exe" Start-Sleep -s 7200 Stop-Process -name
if((Get-Process -Name test -ErrorAction SilentlyContinue) -eq $null){
."C:\Program Files (x86)\test.exe" Start-Sleep -s 7200 Stop-Process -name test}
在单行脚本块中放置多个PowerShell命令时,必须用分号分隔这些命令:
if((Get-Process -Name test -ErrorAction SilentlyContinue) -eq $null){ ."C:\Program Files (x86)\test.exe" ; Start-Sleep -s 7200 ; Stop-Process -name test}
只是为了给Jeff的答案添加一些东西-您可以使用
启动流程
和-PassThru
来确保您正在结束启动的正确流程
if ((Get-Process 'test' -EA SilentlyContinue) -eq $null){
$Process = Start-Process "C:\Program Files (x86)\test.exe" -PassThru
Start-Sleep -Seconds (2*60*60)
$Process | Stop-Process
}
这意味着,如果进程因其他原因而终止,并且手动或通过脚本的另一个副本等重新启动,则该脚本不仅会在两小时后终止它,还会终止正确的进程。您的脚本是否有换行符?如果没有,则启动Sleep,其余的将作为参数传递给test.exe。test.exe是否独立启动?您可能需要启动过程…Test.EXE在一个单独的进程中运行它。我也会考虑这样一个计划任务,这样您就不必担心PuthS壳窗口。您还可以为此设置执行限制。