如何使用示例在Windows中暂停和恢复Powershell和Cmd脚本?

Powershell is as its name suggest powerful language. I have very good features and libraries. In this tutorial we will look how to pause Powershell in different ways and different behaviors.

Powershell顾名思义就是功能强大的语言。 我有很好的功能和库。 在本教程中,我们将研究如何以不同的方式和不同的行为暂停Powershell。

暂停并等待输入 (Pause And Wait Input)

We may want to pause the current shell and wait an input from the user. This is very useful if we need more specific input from the user like selecting a menu or an option. In this example we will read input from users interactive shell which will pause the execution of the shell up to an input is entered by the user.

我们可能要暂停当前的shell,然后等待用户的输入。 如果我们需要用户提供更具体的输入(例如选择菜单或选项),这将非常有用。 在此示例中,我们将从用户交互式shell中读取输入,该输入将暂停shell的执行,直到用户输入了输入。

$User = Read-Host 'Input the user name'

Write-Host "I got it"
Pause And Wait Input
Pause And Wait Input
暂停并等待输入

暂停指定的时间段(Pause For Specified Time Period)

We have another options where we can specify the time as seconds to wait. This will actually sleep the current running interactive process and wait for the given time. After the sleep period end the script will resume executing from point where it left. In this example we will write "I will sleep" in the console and then sleep for 5 seconds after the sleep we will write "I woke up".

我们还有另一个选项,可以将等待时间指定为秒。 实际上,这将使当前正在运行的交互过程Hibernate,并等待给定的时间。 Hibernate期结束后,脚本将从其离开的位置继续执行。 在此示例中,我们将在控制台中写入"I will sleep" ,然后在睡眠后的5秒钟内睡眠,我们将写入"I woke up"

Write-Host "I will sleep"

Start-Sleep -s 5

Write-Host "I woke up"

在MS-DOS中Hibernate (Sleep In MS-DOS)

MS-DOS or cmd also supports for sleeping functionality. This is a bit different from previous examples. This code is more elegant and simple. We will implement code in a single line or one liner. we will issue pause command and print the output to null. We can use this line as in a batch file.

MS-DOS或cmd也支持睡眠功能。 这与以前的示例有些不同。 这段代码更加优雅和简单。 我们将在一行或一行中实现代码。 我们将发出pause命令并将输出打印为null 。 我们可以在批处理文件中使用此行。

$ cmd /c pause | out-null
Sleep In MS-DOS
Sleep In MS-DOS
在MS-DOS中Hibernate
LEARN MORE  How To Determine Installed Powershell Version?
了解更多如何确定已安装的Powershell版本?

翻译自: https://www.poftut.com/pause-resume-powershell-cmd-scripts-windows-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值