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

♀尐吖头ヾ 提交于 2019-11-30 06:53:15

问题


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

How do I elect to 'Run whether 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 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?


回答1:


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



回答2:


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



回答3:


The “Run whether user is logged in or not” option in the Task Scheduler GUI is equivalent to New-ScheduledTaskPrincipal -LogonType S4U.




回答4:


also control Run level check:

RunLevel

Specifies the required privilege level to run tasks that are associated with the principal.

e.g.: "Highest" or "Limited"



来源:https://stackoverflow.com/questions/13965997/set-a-scheduled-task-to-run-when-user-isnt-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!