Task Scheduler script upon login of domain user fails ?!

touqeeranjum 80 Reputation points
2023-02-13T14:22:17.7066667+00:00

Hi,

I have the below script which is run from User A (Local Administrator) and adds task to the scheduler (this part works fine). The server is then joined to a domain.

$userid = Join-Path -Path $env:UserDomain -ChildPath $env:UserName
$action = New-ScheduledTaskAction -Execute Powershell.exe -Argument '-File "C:\Folder\Run.ps1"'
$trigger = New-ScheduledTaskTrigger -AtLogon
$principal = New-ScheduledTaskPrincipal -UserId $userid -RunLevel Highest
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -ErrorAction Stop
Register-ScheduledTask TEST -InputObject $task -ErrorAction Stop | Out-Null

Upon reboot the script autologins (preconfigured) into the domain user and should resume the script it does not run.

If I try and use Set-ScheduledTask to change the user to Domain\User (before a reboot and after joining the domain) with the -User switch, it fails as the current user still is the local Administrator.

After reboot in place of Administrator below, it shows an SID and the task does not run until I manually change it to local account, or domain user.

User's image

Any thoughts on how to get this working on reboot even if accounts change ?!

Any thoughts

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. MotoX80 37,161 Reputation points
    2023-02-14T15:32:38.3333333+00:00

    Try using schtasks.exe.

    SCHTASKS.exe /Create /tn "Test"  /tr "Powershell.exe -noexit -ExecutionPolicy Bypass -File C:\Folder\Run.ps1" /ru interactive /rl highest /sc onlogon
    
    

    Update: I found my own answer. I knew that I had solved this at some point in time.

    Try this.

    $action = New-ScheduledTaskAction -Execute Powershell.exe -Argument '-noexit -ExecutionPolicy Bypass -File "C:\Folder\Run.ps1"'
    $trigger = New-ScheduledTaskTrigger -AtLogon 
    $principal = New-ScheduledTaskPrincipal  -RunLevel Highest -groupid   ".\administrators" 
    $task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -ErrorAction Stop
    Register-ScheduledTask TEST -InputObject $task -ErrorAction Stop | Out-Null
    
    

    https://learn.microsoft.com/en-us/answers/questions/573477/powershell-new-scheduledtasktrigger-cmdlet-atlogon?orderBy=Helpful

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. touqeeranjum 80 Reputation points
    2023-02-14T12:44:03.3+00:00

    Has anyone got a solution for a script to run from a local account and then resume from a domain account ?

    0 comments No comments

  2. touqeeranjum 80 Reputation points
    2023-02-14T12:45:59.0033333+00:00

    Has anyone got a working solution on running a script from a local user, and resume the script from a domain account in a fresh install scenario ?

    0 comments No comments

  3. Limitless Technology 45,126 Reputation points
    2023-02-14T16:40:32.53+00:00

    Hi,

    Thank you for posting your query.

    Kindly follow the steps provided below to resolve your query.

    Enable task history in the Actions pane and verify that the task is being launched. Look for errors.

    If the script is not working correctly, then you need to capture stdout and stderr to see what it's doing. See the answer in this post.

    https://learn.microsoft.com/en-us/answers/questions/1030430/open-a-task-working-in-background.html

    Go to this link for your reference and additional troubleshooting procedures https://learn.microsoft.com/en-us/answers/questions/1032778/task-scheduler-does-not-work-with-the-option-of-no

    If the answer is helpful kindly click "ACCEPT AS ANSWER" and up vote it.

    0 comments No comments

  4. touqeeranjum 80 Reputation points
    2023-02-15T08:47:41.1666667+00:00

    @MotoX80

    Thanks, works as expected, resumes script after reboot from a local account into a domain account.

    Can I clarify that the . represents Any Domain (so .\Administrators would mean Any Domain\Administrator account ?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.