简体   繁体   中英

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?

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?

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?

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?

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?

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?

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?

I was trying to do this on Windows Server 2019 and none of the answers here worked. I suspect that, depending on your environment, security policies, etc., the answer that works for you may differ.

I've read in other places that the -LogonType parameter value S4U to New-ScheduledTaskPrincipal is what checks the Run whether or not the user is logged on checkbox , but that is not the case. I did find one reference that indicated using that logon type will not allow the user running the scheduled task to interact with local files. And for my situation, that was no good.

In my environment, I tried using -LogonType ServiceAccount . I was able to register the task with that. But upon registering the task, the LogonType got updated to Password, AND the account got locked out. So that's not the answer either.

My very first attempt was create all the required Scheduled Task objects (Action, Trigger) and then run Register-ScheduledTask providing the user and password for the task. That also did not work.

One thing you cannot do is call Register-ScheduledTask with both -Principal and -User and -Password parameters—that is an invalid parameter combination. But, this is precisely what you need to do. So how do you do that, then? You create the scheduled task, and then register it:

$TaskName = 'MyTask'
$TaskPath = '\My Task Path`
$Description = 'A useful task.'

# NOTE: The arguments provided here are an illustration only and MAY NOT
#       be appropriate for your environment. Please use appropriate arguments.
# NOTE: Write-Host will result in no visible output since the task is running
#       non-interactively
$TaskAction = New-ScheduledTaskAction -Execute 'powershell.exe' `
    -Arguments '-NoLogo -ExecutionPolicy Bypass -NoProfile ' + `
               '-NonInteractive -C "Write-Host `"I'm doing something very useful...`""'

$TaskPrincipal = New-ScheduledTaskPrincipal -Id 'Author' -UserId 'YourUser' `
    -LogonType Password -RunLevel Limited

$TaskTrigger = New-ScheduledTaskTrigger -Once -At ([DateTime]::Now.AddMinutes(5)) `
    -RepetitionInterval ([TimeSpan]::FromMinutes(5))

$Task = New-ScheduledTask -Description $Description -Action $TaskAction `
    -Principal $TaskPrincipal -Trigger $TaskTrigger

$Task | Register-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath `
    -User 'YourUser' -Password 'p@55w0rd!' # <-- Obviously, don't do this!

In my environment, this has resulted in a scheduled task that runs whether or not the user is logged on, in this case, every 5 minutes.

One way to help yourself is to create a similar scheduled task on your local computer (via the GUI, since the GUI options which correspond to the PowerShell cmdlet parameters aren't always intuitive). Then you can use the schtasks command to query for the scheduled task you created via the GUI and compare it with the scheduled task you created via PowerShell. To use schtasks , for example:

# To get the XML for the task:
schtasks /query /tn '\My Task Path\MyTask' /xml ONE

# To get a nice formatted list of the task properties:
schtasks /query /tn '\My Task Path\MyTask' /v /fo LIST

Hope this helps someone, because it took me a long time to figure this out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM