程序在 30 分钟后开始和结束
Programm Start and End after 30 Minutes
我有一个 Windows MSC(Microsoft 管理控制台),应该会在 30 分钟后结束,因为主内存中的插件非常多。
- 我启动 Powershell 脚本
- PowerShell打开MMC
- PowerShell 将在 30 分钟后终止 MMC
它是如何工作的?
$p = Start-Process mmc -PassThru;
$time = ( Get-Date ).AddMinutes( 30 );
while ( $true )
{
Start-Sleep -Seconds 3; #or Millseconds
if ( $time -lt ( Get-Date ) )
{
if ( -not $p.HasExited )
{
$p.Kill();
}
break;
}
}
感谢评论。
作为替代方案,我建议使用一种不同于 所提出的循环的循环。
$p = Start-Process mmc -PassThru
$expiration = (Get-Date).AddMinutes(30)
do {
Start-Sleep -Milliseconds 200
} until ($p.HasExited -or (Get-Date) -gt $expiration)
if (-not $p.HasExited) { $p.Kill() }
这避免了打破无限循环以及不加选择地继续设定的时间量(即使进程在计时器到期之前结束)。
我有一个 Windows MSC(Microsoft 管理控制台),应该会在 30 分钟后结束,因为主内存中的插件非常多。
- 我启动 Powershell 脚本
- PowerShell打开MMC
- PowerShell 将在 30 分钟后终止 MMC
它是如何工作的?
$p = Start-Process mmc -PassThru;
$time = ( Get-Date ).AddMinutes( 30 );
while ( $true )
{
Start-Sleep -Seconds 3; #or Millseconds
if ( $time -lt ( Get-Date ) )
{
if ( -not $p.HasExited )
{
$p.Kill();
}
break;
}
}
感谢评论。
作为替代方案,我建议使用一种不同于
$p = Start-Process mmc -PassThru
$expiration = (Get-Date).AddMinutes(30)
do {
Start-Sleep -Milliseconds 200
} until ($p.HasExited -or (Get-Date) -gt $expiration)
if (-not $p.HasExited) { $p.Kill() }
这避免了打破无限循环以及不加选择地继续设定的时间量(即使进程在计时器到期之前结束)。