0

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.

usr021986
  • 3,421
  • 14
  • 53
  • 64
  • 1
    Possible duplicate of [Set a Scheduled Task to run when user isn't logged in](https://stackoverflow.com/questions/13965997/set-a-scheduled-task-to-run-when-user-isnt-logged-in) – Charlie Joynt Jan 03 '18 at 22:20
  • i have already checked the answers of the post mentioned in the comment, but non of the answered worked for me. – usr021986 Jan 03 '18 at 22:27
  • Might not be relevant, but the Command you pass to the `-Argument` parameter when creating the action has double quotes around the command BUT ALSO double quotes around the service name. Use the short version of the service name (with no spaces) and drop the quotes around it. – Charlie Joynt Jan 03 '18 at 22:38
  • Next, check the answer to the question I flagged as a possible duplicate. The accepted answer shows that you need to supply username and password; if you create a task manually you will be prompted for username and password, so it is reasonably to expect the cmdlet to need those credentials. – Charlie Joynt Jan 03 '18 at 22:40
  • So i guess your question is not about how to create the task, but more about how to work around the requirement to have a username and password without hard-coding them in the script? – Charlie Joynt Jan 03 '18 at 22:40
  • between the dup posted above, and this post/answer (by me) https://stackoverflow.com/questions/36846688/powershell-run-job-at-startup-with-admin-rights-using-scheduledjob/39378046#39378046 you should be able to tweak your task anyway you want. see all the msdn info for more help as well. – Kory Gill Jan 04 '18 at 00:17

2 Answers2

0

If you are trying to do this... "Run whether user is logged in or not" ... You should run with the system or a service account, not a regular user account and potentially set the 'Run with highest privilege' setting depending on what your are trying to accomplish.

postanote
  • 15,138
  • 2
  • 14
  • 25
  • This is patently false. You can use a normal user account, provided it has the correct level of permissions, privileges, and rights to execute the desired task. See [my answer](https://stackoverflow.com/a/70793765/1470649) on the linked SO Q&A, as well as others' answers to the same Q&A. – fourpastmidnight Jan 20 '22 at 22:12
0

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.

zura
  • 44
  • 6