Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Set a Scheduled Task to run when user isn't logged in

I have been using the Powershell Scheduled Task Cmdlets to create a scheduled task on our servers.

How do I elect to 'Run whether a user is logged in or not using this API?

I've created action, trigger, principal and settings objects, and passed them to Register-ScheduledTask, as below:

$action = New-ScheduledTaskAction -Execute foo.exe -Argument "bar baz" $trigger = New-ScheduledTaskTrigger -Once -At $startTime -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration ([Timespan]::MaxValue) $principal = New-ScheduledTaskPrincipal -UserId "$($env:USERDOMAIN)\$($env:USERNAME)" -LogonType ServiceAccount $settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel  Register-ScheduledTask -TaskName $taskName -TaskPath "\my\path" -Action $action -Trigger $trigger -Settings $settings -Principal $principal 

When I create a scheduled task like this, it defaults to 'Run only when the user is logged on.

This question shows how to do so using COM objects, and this one using schtasks.exe, but how do I do it using the *-ScheduledTask* cmdlets?

like image 681
Peter Avatar asked Dec 20 '12 05:12

Peter


People also ask

How do I run a scheduled task when not logged in?

“You can specify that a task should run even if the account under which the task is scheduled to run is not logged on when the task is triggered. To do this, select the radio button labeled Run whether user is logged on or not. If this radio button is selected, tasks will not run interactively.

How do I set up and manage scheduled tasks 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.


2 Answers

I do not like or approve of the currently highest rated answer as then you have to know your credentials into a script to do this and can't do this from something like Packer or some other system/configuration automation. There is a better/proper way to do this which Aeyoun mentioned but didn't go into details about which is to properly set the principal to run as the system user.

$action = New-ScheduledTaskAction -Execute foo.exe -Argument "bar baz" $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration ([Timespan]::MaxValue) $principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest $settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel  Register-ScheduledTask -TaskName "tasknamehere" -TaskPath "\my\path" -Action $action -Trigger $trigger -Settings $settings -Principal $principal 
like image 139
Farley Avatar answered Sep 26 '22 01:09

Farley


You need to remove $principal and register the task with a user and password:

Register-ScheduledTask -TaskName $taskname `                        -TaskPath "\my\path" `                        -Action $action `                        -Trigger $trigger `                        -User "$env:USERDOMAIN\$env:USERNAME" `                        -Password 'P@ssw0rd' `                        -Settings $settings 
like image 28
Shay Levy Avatar answered Sep 22 '22 01:09

Shay Levy