Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduled job can't run for local accounts #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

codaamok
Copy link

I discovered when trying to run a script block with -AsUser, where the credential is a local user account*, the scheduled job just wouldn't run. powershell.exe would return with an exit code 1 from task scheduler.

Code example is below to repro, however it's important you use a local, non-domain joined, account for the cred.

$cred = Get-Credential
Invoke-CommandAs -ScriptBlock { whoami } -AsUser $cred

It doesn't seem to be an issue for domain accounts, and I don't know enough about Windows like this to rationalise it. However, through debugging I did find once I passed the -RunAs credential to the Register-ScheduledJob in Invoke-ScheduledTask, the scheduled job would be successfully invoked by task scheduler.

* In order to get a local user account to work, you will need to grant the local user account SeBatchLogonRight rights, more info. Can be configured locally w/o GPO using secpol.msc:

image

As an aside, I did a minor cleanup in Invoke-ScheduledTask to initialise a couple of hashtables with a value, rather than initialising the hashtables and then defining values in them - no reason other than "meh".

@mkellerman
Copy link
Owner

Other than the minor cleanup, this seems to be more of a documentation issue, since you're not doing any correction to the code. Am i right?

@codaamok
Copy link
Author

codaamok commented Apr 26, 2023

No, this line is new:

If ($AsUser) { $JobParameters['Credential'] = $AsUser}

It passes the credential for -AsUser to Register-ScheduledJob.

@codaamok
Copy link
Author

codaamok commented May 4, 2023

Thoughts?

@mkellerman
Copy link
Owner

Sorry, i havent had the time to think though this step. Basically, the main credential is used to authenticate to the remote computer. and the AsUser is used to run the script 'as' that user. I think this change would alter the expected behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants