简体   繁体   中英

Schedule task creation with option “”Run whether user is logged in or not“” using powershell

I have below schedule task powershell script which creates a schedule task and runs under a service account.

$taskname = ''myscheduletask'
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "& {stop-service -name "myservicename" -Force}"'
$trigger =  New-ScheduledTaskTrigger  -Once -At (Get-Date).AddSeconds($delayinSeconds)
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname -Description $taskdescription -Force

After creating the schedule task, current default is 'Run only when user is logged on' however i want it to be "Run whether user is logged in or not" .

What settings i am missing here ? Just FYI: i cannot supply the password in this script.

Thanks


Update

At last i ended up using user name and password only which will be retrieved during the script run time.

如果您尝试执行此操作...“无论用户是否登录都运行” ...您应该使用系统或服务帐户(而不是常规用户帐户)运行,并可能设置“以最高特权运行”设置取决于您要完成的工作。

There is not a way around it - you have to provide credentials for that to work. Then of course you don't have to hardcode the password in your script but instead read from a text file using secure strings.

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