Why isn’t my PowerShell script starting the VPN with Task Scheduler?

0
5
Asked By CuriousPenguin822 On

I'm trying to automate my VPN connection using a PowerShell script that checks if I'm connected to my home SSID. If not, it should start my VPN. The script runs fine when I execute it manually, but it doesn't work through Task Scheduler. I can see that Task Scheduler is triggering the script because I included a logging function, but it fails to start the VPN with the Start-Process command when scheduled. Here are my Task Scheduler settings:
- GENERAL: Run whether user is logged on or not, Run with highest privileges.
- TRIGGERS: Begins on an event (Microsoft-Windows-NetworkProfile/Operational, Event ID: 10000).
- CONDITIONS: Runs if any network connection is available.
- SETTINGS: Allows the task to be run on demand and runs as soon as possible if a scheduled start is missed.
Here's the PowerShell code:
```powershell
$homeSSID = "xyzSSID"
$prog = "C:Program Filesxxx.exe"
$currentSSID = (get-netconnectionProfile).Name
if ($currentSSID -ne $homeSSID) {
Start-Process -FilePath $prog
exit
} else {
exit
}
```
What am I doing wrong?

5 Answers

Answered By NetworkNinja45 On

Are you running the script while logged in when testing it manually? Some VPN applications require an interactive session, which may not be happening when the task runs in the background. Try unchecking 'run whether user is logged on or not' and see if that helps.

CuriousPenguin822 -

That seems to have fixed it! I’ll run more tests to confirm, thanks!

SlickUser007 -

Good to hear that! Also, double-check your permissions to ensure everything runs smoothly.

Answered By ObserverX On

It sounds like your VPN might require a user session. If you don’t need the VPN when not logged in, running it only while logged in could work just fine.

Answered By TechScribe99 On

First off, you might want to check the result of the scheduled task using `Get-ScheduledTaskInfo`. It could be something with the task settings itself that's causing issues, especially if it's logging but failing to perform the Start-Process function properly.

MysteryCoder181 -

Here's the result I got:
LastRunTime : 8/16/2025 6:00:38 PM
LastTaskResult : 267014
What does that mean?

TechScribe99 -

That result might indicate a task failure or issue with permissions. Check if the error is related to execution contexts.

Answered By TaskMasterX On

Can you export the scheduled task and share the full XML? That way, we could take a closer look at the configurations.

Answered By DevOpsDynamo On

If your task action points directly to the script, try pointing it to the PowerShell executable instead and pass the script as an argument. This can sometimes resolve issues with execution policies not being set properly.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.