How to set schedule.service “Run whether user is l

2019-01-28 18:11发布

问题:

I need to create a schedule task using Powershell with "Run whether user is logged on or not".

I use the following codes to create a task, and I've checked the members of settings, principal, triggers, and action, but failed to find the setting I need.

$service = new-object -com("Schedule.Service")
$service.connect($servername)
$rootFolder = $service.Getfolder("\")
$taskDefinition = $service.NewTask(0) 
$settings = $taskDefinition.Settings
$principal =  $taskDefinition.Principal
$triggers = $taskDefinition.Triggers
$Action = $taskDefinition.Actions.Create( 0)
$rootFolder.RegisterTaskDefinition( $taksName, $taskDefinition, 6, $taskRunAsuser , $taskRunasUserPwd , 0)

Does anyone know how to do that?

Thanks

回答1:

It was the last parameter in the RegisterTaskDefinition() method... change it from 0 to 1.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa382577(v=vs.85).aspx