Skip to content

Instantly share code, notes, and snippets.

@Rijen
Created January 25, 2022 19:00
Show Gist options
  • Save Rijen/18a8ae87981ce1d278aa8e516b49b172 to your computer and use it in GitHub Desktop.
Save Rijen/18a8ae87981ce1d278aa8e516b49b172 to your computer and use it in GitHub Desktop.
Windows scheduler example
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Support;
/**
* Description of Scheduler
*
* @author rijen
*/
class Scheduler
{
public function getList($dir = '\\digests')
{
$folders = $this->query();
foreach ($folders[$dir] as &$task)
{
$task['data'] = $this->getTaskData($dir . '\\' . $task['name']);
}
return $folders[$dir];
}
private function getTaskData($name)
{
$out = [];
exec('SCHTASKS /TN "' . $name . '" /xml', $out);
return implode('', $out);
}
public function taskToggle($name, $set)
{
$out = [];
exec('SCHTASKS /change /TN "' . $name . '" /' . $set, $out);
return implode('', $out);
}
private function query()
{
$out = [];
exec('SCHTASKS /Query', $out);
$cout = collect($out);
$spl_ids = $cout->filter(function($row)
{
return empty($row);
})->keys();
$folders = [];
foreach ($spl_ids as $k => $id)
{
if ($spl_ids->count() > $k)
{
$fname = $cout[$id + 1];
// print ($id + 4 ) . ' - ' . ($spl_ids[$k++] - ($id + 4));
$data = $cout->slice($id + 4, @$spl_ids[$k + 1] - ($id + 4))->toArray();
$fData = [];
foreach ($data as $task)
{
$fData[] = [
'name' => trim(substr($task, 0, 40)),
'next_run' => trim(substr($task, 41, 22)),
'status' => trim(substr($task, 64, 15)),
];
}
$folders[substr($fname, 8)] = $fData;
}
}
//dd($folders);
return $folders;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment