How Can I Create a Scheduled Task with User Credentials?
Asked by
SunnyBreeze42
I'm trying to set up a scheduled task that runs under the user's credentials, but I'm hitting a snag with a syntax error. It seems like the issue lies with my $Principal setting. I attempted to use both 'NT AuthorityInteractive' and 'BUILTINUsers', but I keep getting the error: "Register-ScheduledTask : No mapping between account names and security IDs was done." What am I doing wrong, and how can I fix this? Here's the code I've been working with:
```powershell
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:ScriptsMyScript.ps1"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 1)
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Minutes 10)
$Principal = New-ScheduledTaskPrincipal -UserId "'BUILTIN\Users'" -LogonType Interactive
Register-ScheduledTask -TaskName "MyDailyTask" -Action $Action -Trigger $Trigger -Principal $Principal -Settings $settings
```
Thanks for the help!
Answers
You might want to try using Schtasks.exe instead. It can create tasks that run as the current user without needing to pass any credentials, which could save you some trouble!
Reply to this answer
Just a heads up, to achieve what you're trying to do, you'll need the user's actual credentials. Neither of the options you've chosen ('NT AuthorityInteractive' or 'BUILTINUsers') qualify as valid user accounts for this kind of setup.
Reply to this answer
Have you considered using the SID for the user group? Something like `$principal = New-ScheduledTaskPrincipal -GroupId "S-1-5-32-545" -Id "R/Powershell" -RunLevel Highest` might work for you! Give that a shot!
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions