Question Locked

Need to deploy a script to create a scheduled Task, script runs but no task

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need to create a scheduled task that runs at startup, I have created a powershell task to create and need to deploy this to all users using Intune.

I have tried to deploy the script but it fails, when i package it as an application, it works and runs sucessfully, but no scheduled task is being created. The file is being copied to the machine, as i can see the new folder being created and the files in it.

Script:

# Set the execution policy to RemoteSigned
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force

#Create a new folder to hold Reg fix
New-item -Path "C:\BigIP" -type Directory

#Copy file accross
Copy-Item -Path "Regkey.ps1" -Destination "C:\BigIP\"

# Define the path to the PowerShell script you want to run at startup
$scriptPath = "C:\BigIP\RegKey.Ps1"

# Define the name of the scheduled task
$taskName = "RunMyScriptAtStartup to add IPEnable Router Key back"

# Create the action to run the PowerShell script
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -WindowStyle Hidden -File "$scriptPath""

# Create the trigger to run the task at startup
$trigger = New-ScheduledTaskTrigger -AtStartup

# Define the principal (user) under which the task will run
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest

# Create the settings for the task
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable

# Register the scheduled task
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings -TaskName $taskName

Why is the task not being created

3 replies

by kees_b

In such a case, you don't run it as a powershell task, but statement for statement manually in powershell. Do all steps work? Then it' should be in the registry.

by mrmohammedawan

I need to deploy a script as it needs to go to over 500 users, via Intune.

I tried to run the script line by line on an affected machine as a normal user (No admin RIghts, all the lines ran fie but then i tried to run

"Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings -TaskName $taskName"

i got the following error

Register-ScheduledTask : Access is denied.
At line:1 char:1
+ Register-ScheduledTask -Action $action -Trigger $trigger -Principal $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTask], CimExcept
ion
+ FullyQualifiedErrorId : HRESULT 0x80070005,Register-ScheduledTask

i though

$principal = New-ScheduledTaskPrincipal -UserId “SYSTEM” -LogonType ServiceAccount -RunLevel Highest

would allow it to run but it does not?

If i try and run Powershell as admin i get the following Error:

Register-ScheduledTask : Cannot create a file when that file already exists.
At line:1 char:1
+ Register-ScheduledTask -Action $action -Trigger $trigger -Principal $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-Scheduled
Task], CimException
+ FullyQualifiedErrorId : HRESULT 0x800700b7,Register-ScheduledTask

but i cannot see the scheduled Task!!!!!

And when i deploy the script via Intune i deploy it using the System account and not the User

This discussion is closed to new replies.