Reputation: 1
powershell running a c++ .exe using start-job cmdlet
$connectionstring = "Server=localhost;Database=testDB;Trusted_Connection=True;"
$connection=New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString=$connectionstring
$connection.Open()
$jobs=@()
$count="SELECT COUNT(*) FROM Tasks WHERE Enabled='1'"
$maxConcurrentJobs=5
$jobsTotalresult=Invoke-Sqlcmd -ServerInstance 'localhost' -Database 'testDB' -Query $count
$jobsTotal=$jobsTotalresult.Column1
$SQLQueryID="SELECT ID FROM Tasks WHERE Enabled=1"
$SQLIDCommand=New-Object System.Data.SqlClient.SqlCommand ($SQLQueryID, $connection)
$SQLQuerySELECT="SELECT ExecPath, Param1, Param2, Param3 FROM Tasks WHERE Enabled=1"
$SQLselectCommand=New-Object System.Data.SqlClient.SqlCommand ($SQLQuerySELECT, $connection)
$tID=$SQLIDCommand.ExecuteScalar()
$SQLReader=$SQLselectCommand.ExecuteReader()
$data = @()
while($SQLReader.Read()){
$row = @{
ExecPath = $SQLReader.GetString(0)
Param1 = $SQLReader.GetString(1)
Param2 = $SQLReader.GetString(2)
Param3 = $SQLReader.GetString(3)
}
$data += New-Object PSObject -Property $row
$jobsTotal=$data.Count
}
$SQLReader.Close()
function JobCompleted($receivedState){
write-host 'JOBCOMPLETED WITH' $receivedState
if($receivedState -eq 'Completed'){
$Status=2
$Note="Task finished successfully '$jid'"
} elseif($receivedState -eq 'Failed') {
$Status=3
$Note="Task failed"
}elseif ($receivedState -ne 'Completed' -or $receivedState -ne 'Failed') {
$Status=9
$Note="NOT DEFINED"
}
$Finished=Get-Date
$SQLQueryUPDATE="UPDATE TaskLogs SET Status=$Status, Finished='$Finished', Note='$Note' WHERE ID IN (SELECT TOP $maxConcurrentJobs ID FROM TaskLogs ORDER BY ID DESC)"
$SQLUPDATEcmd=New-Object System.Data.SqlClient.SqlCommand($SQLQueryUPDATE, $connection)
$SQLUPDATEcmd.ExecuteNonQuery()
}
foreach($currow in $data){
while(($jobs | Where-Object{$_.State -eq 'Running'}).Count -ge $maxConcurrentJobs){
Start-Sleep -Milliseconds 500}
$env:Path=$currow.ExecPath
$job=Start-Job {& $env:Path}
$jobs+=$job
$Created=Get-Date
foreach($runningjob in $job){
if($runningjob.State -eq 'Running'){
$Status=1
$Note="Task is running "
}
$SQLQueryWRITE="INSERT INTO TaskLogs(tID, Status, Created, Finished, Note) Values($tID, $Status, '$Created', null, '$Note')"
$SQLWRITEcmd=New-Object System.Data.SqlClient.SqlCommand($SQLQueryWRITE, $connection)
$SQLWRITEcmd.ExecuteNonQuery()
Register-ObjectEvent -InputObject $runningjob StateChanged -Action {
$currentState=$EventArgs.JobStateInfo.State
JobCompleted($currentState); Unregister-Event $eventsubsciber.SourceIdentifier
}
}
}
Hello, This is my script in which the previous problem I had was running 5 by 5 jobs out of 20. Now I am using a cpp file (.exe) which I wrote that just randomises time, writes it out and pauses the program for that time. I need this test because in the production environment not all jobs will take the same time. But when I run it with this I get jobs that begin and end at the same time and the time written out when I receive a job is the same time for all five jobs, so the time is same per batch, the next five has different time compared to first five but within those five again the time is the same. Can anyone explain what is the situation here and why that happens?
Thanks in advance
Upvotes: 0
Views: 46