Creating Task via Powershell as System32

0

I want to create a Task via Powershell, but i get this Error:

Register-ScheduledTask : Access Denied In C:\Users\4TL4S\Documents\Coding\Bot_Dev\juri_exc\ui\setup_bot_task.ps1:8 Zeichen:1

  • Register-ScheduledTask "Bot_Activation" -InputObject $Task
  • CategoryInfo : PermissionDenied: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTask], CimException
  • FullyQualifiedErrorId : HRESULT 0x80070005,Register-ScheduledTask

I think it has todo with that "Administrator" hasnt got enough rights...How could i fix this? Is it possible to create Task as System32?

This is the Script:

$Action = New-ScheduledTaskAction -Execute "cmd.exe"
$Trigger = New-ScheduledTaskTrigger -At "0am" -DaysOfWeek 1 -Weekly
$Principle = New-ScheduledTaskPrincipal "Administrator" -RunLevel Highest
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Principal $Principle -Trigger $Trigger -Settings $Settings
Register-ScheduledTask "Bot_Activation" -InputObject $Task 

This is the Code which calls the Script:

subprocess.Popen([r'C:\\WINDOWS\\system32\WindowsPowerShell\\v1.0\\powershell.exe',
                         '-ExecutionPolicy',
                         'Unrestricted',
                         './setup_bot_task.ps1'], cwd=os.getcwd())
python
python-3.x
powershell
task
scheduler
asked on Stack Overflow Sep 20, 2020 by lerner_z4 • edited Sep 20, 2020 by lerner_z4

1 Answer

0

You need to run this is an elevated session using the "Run as adminstrator" option.

You can use Start-Process with the parameter -Verb and argument runas. See this test

Start-Process powershell -ArgumentList @'
$Action = New-ScheduledTaskAction -Execute "cmd.exe"
$Trigger = New-ScheduledTaskTrigger -At "0am" -DaysOfWeek 1 -Weekly
$Principle = New-ScheduledTaskPrincipal "Administrator" -RunLevel Highest
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Principal $Principle -Trigger $Trigger -Settings $Settings
Register-ScheduledTask "Bot_Activation" -InputObject $Task 
'@ -Verb runas
answered on Stack Overflow Sep 20, 2020 by Doug Maurer • edited Sep 20, 2020 by Doug Maurer

User contributions licensed under CC BY-SA 3.0