How Can I Create a Scheduled Task with User Credentials?

0
30
Asked By SunnyBreeze42 On

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!

3 Answers

Answered By ScripterDude On

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!

Answered By CodeGuru87 On

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.

Answered By TechNinja123 On

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!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.