0
私はXX分間ループを3秒ごとに実行しようとしています。 whileループの中にはIF文があります。ステートメントが真であれば、私のコマンドを起動して、あらかじめ定義された分だけループを再開してください。条件が満たされたり時間が切れた場合に再起動するPowerShellのタイムループ?
文は、定義された分後に真になることはありません私のコマンドをオフに解雇して、上記設定した時間にもう一度ループを再起動した場合場合。
私はgotoを使ってバッチファイルでこれを行うことができますが、PowerShellでこれを行う方法を見つけ出すことはできません。ここで
は私が達成したいものを表現したものです。
$minutes = 5
while(loop for 5 minutes)
{
if(1 -eq 1)
{
do something
restart the while loop for the minutes defined above
}
start-sleep -seconds 3
}
do something here
restart the while loop for the minutes defined above
編集 -
ここでは、私が思い付いたものです。 PowerShellにスクリプトを書き込もうとしているのは今回が初めてです。これを書くためのよりエレガントな方法があることはほとんど確信しています。
# we need a constant loop going
while(1 -eq 1)
{
# now we need our timed loop. set the timer -seconds 3 (3 seconds right now for testing)
$timeout = new-timespan -seconds 3
$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timeout)
{
# check to see if things are still true
if($something -eq "true")
{
echo "Do nothing."
}
else
{
echo "Do something and restart."
# break out of this timed loop since we want to restart it
break
}
# check every 1 second
start-sleep -seconds 1
}
echo "$something did not equal true in the IF above or the timer has run out. Do something and restart."
# continue restarts the loop
continue
}
I)は、(リセット知りませんでした。しかし、タイマーが0に達した場合、どのようにタイムアウトループを再開しますか?私が考えることができる唯一の方法は、決して止まらないループを包むことです。より良い方法がありますか? – Damien
Mhh。間違った状態をチェックしているようです。あなたは本当にループを終了したくないのですか?その場合はwhile($ true) 'は問題ありませんか?あるいは、適切な時にループを終了するためにチェックできる他の条件がありますか? – whatever
無限ループが必要です。私はバッチファイルでgotoを使っていましたし、 'while($ true)'は私が仮定したのと同じことをするでしょう。ご協力ありがとうございました。 – Damien