简体   繁体   中英

Cannot create Scheduled task with powershell that has option “run only when user is logged on”

Does anyone know how to create a script that will enable me to set the option run only when user is logged on? I have searched for hours but found no good solution. The problem is that this program cannot run in the background and must therefor, from my knowledge run only when user is logged on. Any advice is apreciated

$taskName = 'task name'
$taskDescription = 'task desc'
$executable = "program.exe"
$workingDir = "C:\utv"
$user = "user"
$password = "password"

Write-Host "Configuring scheduled task"
Write-Host " - Executable: $workingDir\$executable"
Write-Host " - User: $user"
Write-Host "pw: $password"

$action = New-ScheduledTaskAction -Execute "$workingDir\$executable"
$trigger = New-ScheduledTaskTrigger -Daily -At 00:01 
$existingTask = Get-ScheduledTask | Where-Object {$_.TaskName -like     $taskName}
$principal = New-ScheduledTaskPrincipal -UserId $user
if ($existingTask) {
    Write-Host "Existing task found, updating..."
    Set-ScheduledTask `
        -TaskName $taskName  `
        -Action $action `
        -Trigger $trigger -User $user -Password $password
}
else {
    Write-Host "Creating new task..."
    Register-ScheduledTask `
        -TaskName $taskName `
        -Description $taskDescription `
        -Action $action `
        -Trigger $trigger -User $user -Password $password
}
Write-Host "Setting Repetition properties"
$newTask = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName}

$newTask.Triggers.repetition.Duration = 'PT23H'
$newTask.Triggers.repetition.Interval = 'PT60M'
$newTask.Principal.LogonType = 
$newTask | Set-ScheduledTask -User $user -Password $password

Write-Host "Done!"

Your script seems to missing some parameters.

$newTask.Principal.LogonType = 

Setting this value to S4U should select "Run whether user is logged in or not" but it may set "Do not store your password..." tick box below it. Calling Register-ScheduledTask with a -User or -Password parameters on it should default to the desired output so New-ScheduledTaskPrincipal is not needed or editing the task Principal.LogonType may not be necessary.

Also, when using -User and -Password parameters on Register-ScheduledTask HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Lsa\\disabledomaincreds must be set to 0 and secpol.msc and the user must be added to secpol.msc > Local Security Policy > User Rights Assignment > Log on as service .

无需设置“用户登录时运行”,因为这是默认设置。

I was looking for the same solution and discovered it through the error I was receiving: Parameter set cannot be resolved using the specified name parameters .

This basically means you have to look at the syntax paragraphs for Set-ScheduledTask , and check which parameters are allowed. If you are using New-ScheduledTaskPrincipal , and registering a principal with the scheduled task, then you'll notice that -User and -Password are not allowed with it:

Set-ScheduledTask
   [[-Principal] <CimInstance>]
   [[-Action] <CimInstance[]>]
   [[-TaskPath] <String>]
   [[-Settings] <CimInstance>]
   [[-Trigger] <CimInstance[]>]
   [-TaskName] <String>
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [<CommonParameters>]

Make sure to set -LogonType (such as Interactive) and -RunLevel for New-ScheduledTaskPrincipal . I found this Windows Developer link for Principal.LogonType property , which gives a better explanation on -LogonType than PowerShell's.

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