【问题标题】:Stopping Powershell command after certain amount of time在一定时间后停止 Powershell 命令
【发布时间】:2019-04-20 01:33:40
【问题描述】:

我想了解如何停止 Powershell 命令 (Invoke-VMscript) 并在经过一定时间后继续执行程序。 (最好不要使用作业)

【问题讨论】:

  • 有太多方法可以实现这一点,无法根据您提供给我们的信息给出答案(没有自己的努力或代码示例)。但是,我想不出任何方法可以在单个线程中执行此操作(因此使用作业、后台工作人员、计时器对象、运行空间等似乎是最低要求)

标签: powershell


【解决方案1】:

您可以在命令中添加计时器/事件。下面是自终止脚本的例子

$timer = New-Object timers.timer
$timer.Interval = 10000 # milliseconds
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Output -Action {Write-Host "Timeout!"; Stop-Process $pid}
$timer.Enabled = $true

# do something

while ($true) {

    sleep 1
    Write-Host (get-date)

}

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2016-12-20
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多