Need to deploy a script to create a scheduled Task, script runs but no task - TechRepublic
Question
November 16, 2024 at 08:07 PM
mrmohammedawan

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

by mrmohammedawan . Updated 1 year, 6 months ago

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

This discussion is locked

All Comments