LoginSignup
0
1

More than 5 years have passed since last update.

Powershellでセマフォもどき

Last updated at Posted at 2018-10-08

まとまった処理単位でsleepを入れる部品です
forやforeachなどのループ処理内にてバックグラウンド処理をしていて、
その戻りが待てないとき、
ある一定の処理数でsleepを入れるために使うセマフォもどき関数なのです

定義


# カウント変数初期化
$script:counting = 0

# 指定したスクリプトブロック${scriptbl}を${number}回実行するごとに${sleeptime}秒待つ、セマフォもどき関数
function do-some-block-and-sleep ([scriptblock]${scriptbl},${number},${sleeptime}) { 
    $script:counting++
        switch ($script:counting) { 
            {$_ % ${number} -ne 0} {& $scriptbl} 
            {$_ % ${number} -eq 0} {& $scriptbl; sleep ${sleeptime}; $script:counting = 0}  
        }
}

用例

# 例1
# 1〜300までの数字を1つずつechoする処理をセマフォもどきに渡し、100ごとに2秒待つ
for ($i=1;$i -le 300;$i++) {
                          #  "echo $i" を 100回するごとに 3秒休む
    do-some-block-and-sleep  {echo $i} 100 3
}

# 例2 # (これがやりたかった)
Get-ADOrganizationalUnit -Filter 'Name -like "*"'| %{Get-GPInheritance -target $_} | %{$_.gpolinks} |%{$gpoid = $_.Gpoid;$displayname = $_.Displayname;do-some-block-and-sleep {Get-GPOReport -Guid $gpoid -ReportType Html -Path c:\gpohtml\$($displayname).html} 30 5}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1