循环脚本if -loop $ true由命名参数传递

时间:2012-03-30 16:54:56

标签: loops powershell

如果以myscript.ps1 -loop -seconds 20开头,我想循环播放我的脚本 (秒将是每个循环之间的开始 - 睡眠)。

  • 脚本非常复杂,我点源其他两个脚本文件 (一个用于功能,一个用于设置)。
  • 我在脚本中提示输入$password = Read-Host "Enter password" -AsSecureString的密码,所以我认为我不能使用kicker / launcher-script而不会丢失$ password变量,这样我就可以输入每个循环的pwd ..

如果将-loop作为命名参数传递,我将如何循环播放脚本并仅循环?

我应该将整个脚本放在while循环中还是最好的方式是什么?

1 个答案:

答案 0 :(得分:3)

使用do .. while循环,例如:

# Dot source other scripts
# Ask for and store password in a variable
do {
    ... # this stuff happens once or until Ctrl+C depending on $loop switch
    if ($loop) { Start-Sleep -sec $seconds }
} while ($loop)

虽然不是循环切换,但你最好使用默认为1的循环计数?