Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Create Scheduled Task to run as local system / service

Can anyone tell me how to create a scheduled task using powershell that runs as the local system or local service?

Everything works great except the call to ITaskFolder.RegisterTaskDefinition().

If I pass in $null, or "", than the call bombs saying invalid username or password. Any thoughts"

$Rootfolder.RegisterTaskDefinition("Test", $Taskdef, 6, "LOCAL SERVICE", "", 3)

like image 666
devlife Avatar asked Jan 04 '10 16:01

devlife


People also ask

How do I create a scheduled task in PowerShell?

Open Start. Search for PowerShell, right-click the top result, and select the Run as administrator option. (Optional) Type the following command to confirm the task exists and press Enter: Get-ScheduledTask -TaskName "TAKS-NAME" In the command, make sure to replace "TAKS-NAME" with the name of the task.

Can I run a PowerShell script from Task Scheduler?

Use the task scheduler to schedule PowerShell scriptsUsing the task scheduler is one of the easiest ways to schedule PowerShell scripts. To do this: Right-click the Start button and choose “Run” In the dialog box, type “taskschd.


3 Answers

For those who can use PowerShell 3.0 on Windows 8 or Windows Server 2012, new cmdlets will let you do it in a simple way when registering your scheduled task with the cmdlet Register-ScheduledTask and as argument -User "System"

Here is a scheduled task created entirely with PS, its purpose is to restart a service My Service, using the SYSTEM account, 3 minutes after the system has started:

$taskname = "Restart My Service"
$taskdescription = "Restart My Service after startup"
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' `
  -Argument '-NoProfile -WindowStyle Hidden -command "& Restart-Service -displayname \"My Service\""'
$trigger =  New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -minutes 3)
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 2) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname -Description $taskdescription -Settings $settings -User "System"

NB: you will need to run powershell as an administrator for that script.

like image 192
Djidiouf Avatar answered Oct 14 '22 23:10

Djidiouf


I think you would need to use "nt authority\localservice" as the user name.

Kindness,

Dan

like image 44
Daniel Elliott Avatar answered Oct 14 '22 22:10

Daniel Elliott


This code snippet will use the PowerShellPack's Task Scheduler module to schedule a task to run as SYSTEM immediately:

New-Task |
    ForEach-Object {
        $_.Principal.Id = "NTAuthority\SYSTEM"
        $_.Principal.RunLevel = 1
        $_
    } |
    Add-TaskAction -Script {
        "SystemTask" > C:\myTest.txt
    } |
    Add-TaskTrigger -OnRegistration |
    Register-ScheduledTask SystemTask
like image 38
Start-Automating Avatar answered Oct 14 '22 21:10

Start-Automating