while($true)
{
#Invoke-Expression -Command $PSScriptRoot\CyberArkAutoChange.ps1
$ChildItems = ""
$ChildItems = Get-ChildItem -Path 'D:\WEBROOT\CyberArkRequest\data'
if($ChildItems.count -gt 0)
{
write-host "neue Files" -ForegroundColor DarkMagenta
if($ChildItems.Count -eq 1)
{
$RandomGenerator = 1
Write-host "eis"
}
elseif($ChildItems.Count -eq 2)
{
$RandomGenerator = 2
}
elseif($ChildItems.Count -eq 3)
{
$RandomGenerator = 3
}
elseif($ChildItems.Count -ge 3)
{
$RandomGenerator = 4
}
# Read the input and queue it up
$jobInput = Get-Random -InputObject $ChildItems -Count $RandomGenerator
# How many jobs we should run simultaneously
if($jobInput.count -eq 1)
{
Write-host "1 Job"
$maxConcurrentJobs = 1;
}
elseif($jobInput.count -eq 2)
{
Write-host "2 Jobs"
$maxConcurrentJobs = 2;
}
elseif($jobInput.count -eq 3)
{
Write-host "3 Jobs"
$maxConcurrentJobs = 3;
}
elseif($jobInput.count -eq 4)
{
Write-host "4 Jobs"
$maxConcurrentJobs = 4;
}
$queue = [System.Collections.Queue]::Synchronized( (New-Object System.Collections.Queue) )
foreach($item in $jobInput)
{
$queue.Enqueue($item)
}
# Function that pops input off the queue and starts a job with it
function RunJobFromQueue ($cred)
{
#write-host $cred
if( $queue.Count -gt 0)
{
write-host $cred
$j = Start-Job -ArgumentList @($queue.Dequeue(), $cred) -Name "Cyberark" -ScriptBlock {
param($x, $cred);
write-host "============="
write-host $cred -ForegroundColor Green
write-host $x
Invoke-Expression "D:\Scheduled_Tasks\CyberArkAutoChange\CyberArkAutoChange.ps1 $x $cred"
}
Register-ObjectEvent -InputObject $j -EventName StateChanged -Action { RunJobFromQueue; Unregister-Event $eventsubscriber.SourceIdentifier; Remove-Job $eventsubscriber.SourceIdentifier } | Out-Null
}
}
# Start up to the max number of concurrent jobs
# Each job will take care of running the rest
for( $i = 0; $i -lt $maxConcurrentJobs; $i++ )
{
$PACLIUserCount = 1 + $i
#write-host "CHGPWCLI_$test"
RunJobFromQueue "CHGPWCLI_$PACLIUserCount"
}
$jobs = Get-Job *
if(($jobs.name).Contains("Cyberark"))
{
Wait-Job -Name Cyberark
Remove-Job -Name "Cyberark"
}
else
{
Write-host "kein File zur Verarbeitung verfügbar" -ForegroundColor Green
}
#Wait-Job -Name "Cyberark"
}
sleep 2
#pause;
}