Skip to content

Instantly share code, notes, and snippets.

@kohnakagawa
Last active June 8, 2018 13:37
Show Gist options
  • Save kohnakagawa/582f1345249b12d1703eb26659be1be2 to your computer and use it in GitHub Desktop.
Save kohnakagawa/582f1345249b12d1703eb26659be1be2 to your computer and use it in GitHub Desktop.
ディレクトリ配下の実行ファイルをリストアップして実行するためのスクリプト
#requires -Version 2
function execForEach($files, $beg, $end, $unroll) {
$maximumRuntimeSeconds = 3
$procs = @(1..$unroll)
for ($i = $beg; $i -lt $end; $i += $unroll) {
for ($j = 0; $j -lt $unroll; $j++) {
$procs[$j] = Start-Process -NoNewWindow -FilePath $files[$i + $j].FullName -PassThru
}
try {
for ($j = 0; $j -lt $unroll; $j++) {
$procs[$j] | Wait-Process -Timeout $maximumRuntimeSeconds -ErrorAction Stop
}
}
catch {
Write-Warning -Message "A timeout is reached."
for ($j = 0; $j -lt $unroll; $j++) {
$procs[$j] | Stop-Process -Force
}
}
}
}
$num_unroll = 10
$files = Get-ChildItem -Path ".\test"
$cnt = $files.Count
$rem = [int]($cnt / $num_unroll) * $num_unroll
execForEach $files 0 $rem $num_unroll
execForEach $files $rem $cnt 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment