sâmbătă, 18 august 2018

PowerShell script in schedule task cannot start a second script

In my current firstscript.ps1 i`m checking if i got allready a job started and running, if not will will give a job start for a second script.
This firstscript.ps1 i got in Schedule Task
$Env:SCRIPT_HOME - is a User Enviroment for a path

contenet example for firstscript.ps1

$maxConcurrentJobs = 1
if ((Get-Job -State 'Running').Count -eq $maxConcurrentJobs) {
 $joburicurente = (Get-Job -State 'Running').Count
 Write-Host -ForegroundColor RED  “Mai sunt $joburicurente"
} Else {
 $joburicurente = (Get-Job -State 'Running').Count

 Write-Host -ForegroundColor GREEN “ Putem rula job-ul. Mai sunt $joburicurente"
 #start-job -filepath $Env:SCRIPT_HOME \monitor\secondscript.ps1


$job_result = start-job -filepath $Env:SCRIPT_HOME \monitor\secondscript.ps1

$job_id = $job_result.id
while ($job_result.state -match "Running"){
Start-Sleep -seconds 5
$job_result = get-job $job_id
}

}

Tested with PowerShell 4