Parallel processing using Scriptblock not working from windows task scheduler

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
June 12th, 2014 6:17pm

I modified your code to open notepad and set $command to be a list of 6 files to open (anything above the $MaxThreads would do) so I could test it.

Running from ISE works fine, but so does running from Task Scheduler.

So the problem doesn't appear to be in the code directly, which leaves either something with your commands/executable or something with the scheduled task itself.

When you run it in ISE, do you run it as admin? Do you have the scheduled task to set to run with elevated rights?

Have you confirmed that when you run it as a scheduled task there are no lingering perfcalc.exe processes? It could be that it succeeds in opening the first them until it hits the $MaxThreads but the processes don't close, so it just sits there waiting for the number of current running jobs to be lower than $MaxThreads, which never happens - without more information I would lean towards this.

Have you tried running it in ISE using the same user that you're using to run it on a scheduled task?

I know it's unlikely that almost an year later we'll get answers to this, but just in case you're still having this issue... reply back here :)

Free Windows Admin Tool Kit Click here and download it now
April 14th, 2015 6:57pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics