Run Scheduled Task as SYSTEM only when user is logged in

Anonymous
2021-02-04T21:35:41.843+00:00

Hi

I have a powershell script I want to run interactively so that it interacts with the logged in user. The script has to run as an Admin because it needs to change reg keys. I have created a scheduled task in GPP that runs SystemUI.exe and calls the powershell script. I have set it to run as SYSTEM and only when the user is logged in.

The task works exactly how I want it to when the user is logged in but unfortunately it also tries to run when the user isnt logged in and it fails of course because there is no one to interact with the script. I have selected the "Only run when user is logged in" when I have set up the scheduled task in the GPP but when I view the task on the PC, I see that it changes it to "whether or not the user is logged on".

Is there any way around this ? Is there another account I can be using ?

Is there another way of doing it, i.e running the task as the logged in user but somehow calling the powershell using the SYSTEM account ?

This is on Win 10.

Thanks

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,664 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Gary Blok 1,736 Reputation points
    2021-02-05T04:57:38.787+00:00

    If you have Configuration Manager, you could leverage the software center to do this instead of a scheduled task.

    for your scheduled task, could you add logic to your powershell script that you're running that does a check for a logged on user, and if a user isn't logged on, then it exits?


  2. MotoX80 31,826 Reputation points
    2021-02-05T15:06:30.987+00:00

    I have set it to run as SYSTEM and only when the user is logged in.

    In that context, the "user" is SYSTEM, not any desktop user. To get it to run when a user logs on, you should set it to run as INTERACTIVE.

    I have no experience with SystemUI.exe. I don't know what requirements/restrictions it has in order to properly function.

    Another option would be to modify the scripts that I provided in your prior post. In the Server script, change the Client task to run at user logon.

    SCHTASKS /Create /tn "ClientTask"  /tr "Powershell.exe -File ""$ScriptFolder\Client.ps1""" /ru interactive /sc onlogon /f  
    

    So the Server task will launch, and create the Client task with a trigger to run at logon time. If a user is logged on at the time, the schtasks /run will launch it for them immediately. If no user is logged on, I would expect that the ONLOGON trigger would start the task when the next user logs in. But I have not tested that.

    The Server script will just sit and wait until a Client.msg file shows up.

    Check the Server task settings for "Stop the task if it runs longer than:" setting. Adjust for however long you want to wait.

    https://learn.microsoft.com/en-us/answers/questions/246173/scheduled-task-to-launch-powershell-script-in-syst.html