如何在 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.