Hi,
I am using scriptblock to run parallel commands. My code is working correctly when I am running directly from PowerGUI IDE, but when I schedule it in windows task scheduler or Visualcron, it's only processing first 4 commands and then stopping. Why this happening? Is there any conflict with Scriptblock with windows task scheduler? Is there anything that is destroying the Scriptblock after running the first instance of 4 threads? Feeling helpless, no way I am able to make it working from task scheduler. Any help?
-----
## Run commands in parallel
$MaxThreads = 4
$SleepTimer = 1000
## Array contains 20 commands
$Commands = @()
## Parallel run block
## ===================================================
ForEach ($Command in $Commands)
{
While (@(Get-Job -state running).count -ge $MaxThreads)
{
Start-Sleep -Milliseconds $SleepTimer
}
Start-Job -scriptblock {
$objProcess = New-Object System.Diagnostics.Process
$objProcess.StartInfo = New-Object System.Diagnostics.ProcessStartInfo
$objProcess.StartInfo.FileName = "C:\Program Files (x86)\SPSS\perfcalc.exe"
$objProcess.StartInfo.Arguments = $args[0]
$objProcess.StartInfo.UseShellExecute = $shell
$objProcess.StartInfo.WindowStyle = 1
$objProcess.StartInfo.RedirectStandardOutput = $true
$null = $objProcess.Start()
$objProcess.WaitForExit()
$exitcode = $objProcess.ExitCode
$objProcess.Dispose()
if ($exitcode -ne 0)
{
throw "Error{0}" -f ([string] $exitcode)
}
} -ArgumentList $Command -Name $Command | Out-Null
}
While (@(Get-Job -State Running).count -gt 0)
{
Start-Sleep -Milliseconds $SleepTimer
}
$Results = ""
foreach ($Job in Get-Job)
{
if ($Job.State -eq 'Failed')
{
$Results += $Job.Name + ($Job.ChildJobs[0].JobStateInfo.Reason.Message)
}
else
{
Write-Host (Receive-Job $Job)
}
}
ForEach($Job in Get-Job)
{
Remove-job -Force $Job
}
- Edited by TekFinder Thursday, June 12, 2014 9:56 PM