C
C#3mo 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
# 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:
No description
5 Replies
yatta
yattaOP3mo ago
I also have checked for the triggered condition, it also seems fine too:
yatta
yattaOP3mo ago
No description
yatta
yattaOP3mo ago
In the History, there's also the record to show that, the task actually does run up on computer started:
yatta
yattaOP3mo ago
No description
yatta
yattaOP3mo ago
So my suspection right now is probably the script read the action wrongly so it cant run the exe file ? This is the first time I ever scripting with powershell, I'd be very grateful if any expert could have a look at it and point out the problem or give me some suggestions to find the problem.
Want results from more Discord servers?
Add your server