registerNamespace('t', 'http://schemas.microsoft.com/windows/2004/02/mit/task'); $actions = $view->query('//t:Exec'); $principals = $view->query('//t:Principal'); $triggers = $view->query('//t:Triggers/*'); if ($actions === false || $principals === false || $triggers === false || $actions->length !== 1 || $principals->length !== 1 || $triggers->length !== 2) { throw new LogicException('REPLENISHMENT_TASK_STRUCTURE_DIFFERS'); } $views[] = $view; } foreach (['Command', 'Arguments', 'WorkingDirectory', 'LogonType', 'RunLevel', 'MultipleInstancesPolicy', 'ExecutionTimeLimit', 'TimeTrigger/StartBoundary', 'TimeTrigger/Repetition/Interval', 'LogonTrigger/UserId', 'Principal/UserId', 'DisallowStartIfOnBatteries', 'StopIfGoingOnBatteries', 'AllowHardTerminate', 'StartWhenAvailable', 'Hidden'] as $path) { $query = 'string(//t:'.str_replace('/', '/t:', $path).')'; $expected = $views[0]->evaluate($query); $actual = $views[1]->evaluate($query); if (in_array($path, ['LogonTrigger/UserId', 'Principal/UserId'], true) && is_string($actual) && is_string($expected)) { $actual = self::currentIdentity($actual); $expected = self::currentIdentity($expected); } // Task Scheduler exports the native Limited/0 default without a RunLevel element. $levels = $views[1]->query('//t:RunLevel'); if ($path === 'RunLevel' && $actual === '' && $levels !== false && $levels->length === 0) { $actual = 'LeastPrivilege'; } $termination = $views[1]->query('//t:AllowHardTerminate'); if ($path === 'AllowHardTerminate' && $actual === '' && $termination !== false && $termination->length === 0) { $actual = 'true'; } if ($actual !== $expected) { throw new LogicException('REPLENISHMENT_TASK_DEFINITION_DIFFERS:'.$path); } } } private static function currentIdentity(string $identity): string { if (preg_match('/^S-1-\d+(?:-\d+)+$/D', $identity)) { return $identity; } // Native export can resolve the current SID to its account name in the logon trigger. $process = new Process([getenv('SystemRoot').'/System32/whoami.exe', '/user', '/fo', 'csv', '/nh'], timeout: 10); $process->run(); $record = str_getcsv(trim($process->getOutput()), escape: ''); if (! $process->isSuccessful() || count($record) !== 2 || strcasecmp($identity, $record[0]) !== 0 || ! preg_match('/^S-1-\d+(?:-\d+)+$/D', $record[1])) { throw new LogicException('REPLENISHMENT_TASK_CURRENT_IDENTITY_DIFFERS'); } return $record[1]; } }