如何在 PowerShell 中使用 Timeout 命令?


Timeout.exe 实际上是一个cmd 命令,也可以用于 PowerShell。让我们看看有关 Timeout 命令的帮助。

timeout /?

如果我们看到超时参数列表,我们可以使用 /T ,它表示以秒为单位的时间,以及 /NoBreak 命令,该命令将忽略指定时间内的任意键。

示例

Write-Output "Timeout is for 10 seconds"
Timeout /T 10
Write-Output "This line will be executed after 10 seconds if not interuptted"

输出

PS C:\> C:\Temp\TestPS1.ps1
Timeout is for 10 seconds
Waiting for 5 seconds, press a key to continue ...

请注意:在上面的示例中,用户可以使用任意键中断超时秒,以禁止用户中断,请使用 /NoBreak 开关。

示例

Write-Output "Timeout is for 10 seconds"
Timeout /NoBreak 10
Write-Output "This line will be executed after 10 seconds"

输出

PS C:\> C:\Temp\TestPS1.ps1
Timeout is for 10 seconds
Waiting for 2 seconds, press CTRL+C to quit ...

如果你按 Ctrl+C,它将终止整个脚本执行。

更新于: 2021 年 1 月 4 日

超过 18K 次浏览

开启你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.