Powershell:设置计划任务以在用户未登录时运行 [英] Powershell: Set a Scheduled Task to run when user isn't logged in
问题描述
我一直在使用 Powershell 计划任务 Cmdlets 来创建我们服务器上的计划任务.
I have been using the Powershell Scheduled Task Cmdlets to create a scheduled task on our servers.
我如何选择无论用户是否使用此 API 登录都运行?
How do I elect to 'Run whether a user is logged in or not using this API?
我已经创建了 action
、trigger
、principal
和 settings
对象,并将它们传递给 Register-ScheduledTask
,如下:
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.
这个问题展示了如何使用 COM 对象和 这个 使用 schtasks.exe,但我如何使用 *-ScheduledTask*
cmdlet 执行此操作?
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?
推荐答案
您需要删除 $principal 并使用用户和密码注册任务:
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
这篇关于Powershell:设置计划任务以在用户未登录时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!