[英]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.我一直在使用Powershell 计划任务 Cmdlet在我们的服务器上创建计划任务。
How do I elect to 'Run whether a user is logged in or not using this API?如何选择“无论用户是否使用此 API 登录都运行”?
I've created action
, trigger
, principal
and settings
objects, and passed them to Register-ScheduledTask
, as below:我创建了
action
、 trigger
、 principal
和settings
对象,并将它们传递给Register-ScheduledTask
,如下所示:
$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? 这个问题展示了如何使用 COM 对象来做到这一点,而这个问题使用 schtasks.exe,但是如何使用
*-ScheduledTask*
cmdlet 来做到这一点?
I have been using the Powershell Scheduled Task Cmdlets to create a scheduled task on our servers.我一直在使用Powershell计划任务Cmdlet在我们的服务器上创建计划任务。
How do I elect to 'Run whether a user is logged in or not using this API?如何选择“运行是否使用此API登录用户?
I've created action
, trigger
, principal
and settings
objects, and passed them to Register-ScheduledTask
, as below:我创建了
action
, trigger
, principal
和settings
对象,并将它们传递给Register-ScheduledTask
,如下所示:
$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? 这个问题说明了如何使用COM对象,而这个使用schtasks.exe,但是如何使用
*-ScheduledTask*
cmdlet做到这一点?
I have been using the Powershell Scheduled Task Cmdlets to create a scheduled task on our servers.我一直在使用Powershell计划任务Cmdlet在我们的服务器上创建计划任务。
How do I elect to 'Run whether a user is logged in or not using this API?如何选择“运行是否使用此API登录用户?
I've created action
, trigger
, principal
and settings
objects, and passed them to Register-ScheduledTask
, as below:我创建了
action
, trigger
, principal
和settings
对象,并将它们传递给Register-ScheduledTask
,如下所示:
$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? 这个问题说明了如何使用COM对象,而这个使用schtasks.exe,但是如何使用
*-ScheduledTask*
cmdlet做到这一点?
I have been using the Powershell Scheduled Task Cmdlets to create a scheduled task on our servers.我一直在使用Powershell计划任务Cmdlet在我们的服务器上创建计划任务。
How do I elect to 'Run whether a user is logged in or not using this API?如何选择“运行是否使用此API登录用户?
I've created action
, trigger
, principal
and settings
objects, and passed them to Register-ScheduledTask
, as below:我创建了
action
, trigger
, principal
和settings
对象,并将它们传递给Register-ScheduledTask
,如下所示:
$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? 这个问题说明了如何使用COM对象,而这个使用schtasks.exe,但是如何使用
*-ScheduledTask*
cmdlet做到这一点?
I have been using the Powershell Scheduled Task Cmdlets to create a scheduled task on our servers.我一直在使用Powershell计划任务Cmdlet在我们的服务器上创建计划任务。
How do I elect to 'Run whether a user is logged in or not using this API?如何选择“运行是否使用此API登录用户?
I've created action
, trigger
, principal
and settings
objects, and passed them to Register-ScheduledTask
, as below:我创建了
action
, trigger
, principal
和settings
对象,并将它们传递给Register-ScheduledTask
,如下所示:
$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? 这个问题说明了如何使用COM对象,而这个使用schtasks.exe,但是如何使用
*-ScheduledTask*
cmdlet做到这一点?
I have been using the Powershell Scheduled Task Cmdlets to create a scheduled task on our servers.我一直在使用Powershell计划任务Cmdlet在我们的服务器上创建计划任务。
How do I elect to 'Run whether a user is logged in or not using this API?如何选择“运行是否使用此API登录用户?
I've created action
, trigger
, principal
and settings
objects, and passed them to Register-ScheduledTask
, as below:我创建了
action
, trigger
, principal
和settings
对象,并将它们传递给Register-ScheduledTask
,如下所示:
$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? 这个问题说明了如何使用COM对象,而这个使用schtasks.exe,但是如何使用
*-ScheduledTask*
cmdlet做到这一点?
I have been using the Powershell Scheduled Task Cmdlets to create a scheduled task on our servers.我一直在使用Powershell计划任务Cmdlet在我们的服务器上创建计划任务。
How do I elect to 'Run whether a user is logged in or not using this API?如何选择“运行是否使用此API登录用户?
I've created action
, trigger
, principal
and settings
objects, and passed them to Register-ScheduledTask
, as below:我创建了
action
, trigger
, principal
和settings
对象,并将它们传递给Register-ScheduledTask
,如下所示:
$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? 这个问题说明了如何使用COM对象,而这个使用schtasks.exe,但是如何使用
*-ScheduledTask*
cmdlet做到这一点?
I was trying to do this on Windows Server 2019 and none of the answers here worked.我试图在 Windows Server 2019 上执行此操作,但此处的答案均无效。 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.我在其他地方读到
-LogonType
参数值S4U
到New-ScheduledTaskPrincipal
是检查用户是否登录的运行复选框,但事实并非如此。 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
.在我的环境中,我尝试使用
-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.
但是在注册任务后,LogonType 更新为 Password,并且帐户被锁定。 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.我的第一次尝试是创建所有必需的计划任务对象(操作、触发器),然后运行
Register-ScheduledTask
提供任务的用户和密码。 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.您不能做的一件事是使用
-Password
和-User
以及-Principal
参数调用Register-ScheduledTask
- 这是无效的参数组合。 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.在我的环境中,这导致了无论用户是否登录都会运行的计划任务,在这种情况下,每 5 分钟运行一次。
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).帮助自己的一种方法是在本地计算机上创建一个类似的计划任务(通过 GUI,因为与 PowerShell cmdlet 参数对应的 GUI 选项并不总是直观的)。 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.然后,您可以使用
schtasks
命令查询您通过 GUI 创建的计划任务,并将其与您通过 PowerShell 创建的计划任务进行比较。 To use schtasks
, for example:要使用
schtasks
,例如:
# 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.希望这对某人有所帮助,因为我花了很长时间才弄清楚这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.