C#C
C#15mo ago
yatta

Powershell script to automate run exe file

I have this script that is designed to automatically run an exe file in administrator mode everytime the user turn on the computer and login.
# Get the directory of the script
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition

# Define the path to the exe
$exePath = Join-Path -Path $scriptPath -ChildPath "MyExeFile.exe"

# Check if the exe exists
if (-not (Test-Path $exePath)) {
    Write-Error "MyExeFile.exe not found in the script directory."
    exit 1
}

# Name for the scheduled task
$taskName = "Run Exe File on Login"

# Create a new scheduled task action
$action = New-ScheduledTaskAction -Execute $exePath

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

# Set up principal to run with highest privileges
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest

# Create the scheduled task
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal

# Register the scheduled task
Register-ScheduledTask -TaskName $taskName -InputObject $task -Force

# Run the task immediately
Start-ScheduledTask -TaskName $taskName

When I run the script as administrator, it runs and there's no problem appeared but when I restart the computer and then loging in, the exe file isn't run at all.

I found out that: The Script does run, however, in the Task Scheduler Library it set as Queue, so I need to make it run manually in Task Scheduler Library.

But even so, when I try to restart the computer, the exe file still isnt executed.

When I check for the action, it seems right since I did run that action in cmd myself and the exe file is run as expect.

The action is showed as below:
VCUzuMgt.png
Was this page helpful?