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

前端 未结 5 1200
一整个雨季
一整个雨季 2020-12-05 07:02

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

相关标签:
5条回答
  • 2020-12-05 07:05

    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
    
    0 讨论(0)
  • 2020-12-05 07:11

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

    0 讨论(0)
  • 2020-12-05 07:12

    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"

    0 讨论(0)
  • 2020-12-05 07:27

    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
    
    0 讨论(0)
  • 2020-12-05 07:29

    Once you have the task set up in the gui, run this

    $task = Get-ScheduledTask "test task for notepad"
    $task.Principal.LogonType = "Password"
    Set-ScheduledTask $task
    
    0 讨论(0)
提交回复
热议问题