Share via

Intune or Endpoint script using Register-ScheduledTask to create scheduled task

Preston Cole 6 Reputation points
2021-02-09T16:30:10.42+00:00

Is this even possible?

I want to use MS Endpoint/Intune to run a powershell script that creates a scheduled task on a Windows 10 machine.

The script I run is:

$taskName = "Task-Name"
$action = New-ScheduledTaskAction -Execute "Rundll32.exe" -Argument "user32.dll,LockWorkStation"
$trigger = New-ScheduledTaskTrigger -Daily -AT "23:00"
$settings = New-ScheduledTaskSettingsSet
$inputObject = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings
Register-ScheduledTask -TaskName $taskName -InputObject $inputObject

The task will lock the machine at a specific time.
When I paste the script contents into a local powershell, the scheduled task is created.

When I use Endpoint to push the script to a remote machine, it fails.

The Sensor log file on the remote machine reports:
<![LOG[[LogonTaskHelper] Cannot remove task from task scheduler... It does not exists.]LOG]!
The spelling of exists is correct!

Remote Windows 10 is Version 20H2

Am I trying to achieve the impossible?

Cheers

Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments

3 answers

Sort by: Most helpful
  1. Odd Solberg 1 Reputation point
    2021-05-23T00:25:32.873+00:00

    Could it be that you have not spesified user? Maby if you add:

    $TaskPrincipal = New-ScheduledTaskPrincipal "$env:USERNAME"
    or
    $TaskPrincipal = New-ScheduledTaskPrincipal "System"

    And add the -Principal to the register-scheduledtask command at the end?
    Register-ScheduledTask -TaskName $taskName -InputObject -Principal $inputObject $TaskPrincipal

    Take a look at this article...
    https://www.patrickvanbemmelen.nl/set-a-powershell-script-or-other-file-as-scheduled-task-with-endpoint-manager/

    Was this answer helpful?

    0 comments No comments

  2. Preston Cole 6 Reputation points
    2021-02-12T08:51:36.163+00:00

    Thanks MarileeTurscak

    Sadly this does not talk about using MS Endpoint and scripts.

    My powershell script works if I run it locally, that's all good. The issue I have is that when I use MS Endpoint to push the script to a machine, it fails. I am able to push other powershell scripts to machines that change registry entries etc, but this scheduled task creation fails.

    Was this answer helpful?

    0 comments No comments

  3. Marilee Turscak-MSFT 37,391 Reputation points Microsoft Employee Moderator
    2021-02-11T20:42:16.663+00:00

    Based on a search through other threads it looks like this has been done. I don't believe there is official guidance around this but there are some examples online. https://www.reddit.com/r/Intune/comments/dv1nzn/script_that_will_create_scheduled_task_on_all/

    Here is a blog that has an example of using scheduled tasks to create tasks that run on Log On.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.