What's new
Carbonite

South Africa's Top Online Tech Classifieds!
Register a free account today to become a member! (No Under 18's)
Home of C.U.D.

Running a Powershell Script from Task Sheduler

iamgigglz

VIP
VIP Supporter
Rating - 100%
311   0   0
Joined
Aug 19, 2014
Messages
8,684
Reaction score
2,335
Points
10,155
Location
Parkhurst
I have a small NUC PC that runs my Plex server etc. It has mapped network drives pointing to my NAS.
My issue is after a power failure or Windows restart my mapped drives show as disconnected. I have to remote into the machine and double-click on the drives to reconnect. It's a well documented issue with Windows.

The solution I'm trying to implement is running a powershell script shortly after log-on that reconnects the drives.
The script works perfectly if I right click -> run with powershell, but running it from within task scheduler doesn't work.
The blue powershell window briefly pops up but the drives don't reconnect.

I'm logged on as an Administrator user (no password) and I've set the sheduled action to run with highest privileges.

Any ideas?
 
Just a rough thought. Have you tried to create a bat file / powershell equivalent. Then run the bat file with task scheduler with highest privileges ie Admin.
 
Start-up script no bueno ?

 
My task scheduler actions freak out when the "Run whether user is logged on or not" is ticked, even when I'm logged in, so might be that.
On windows10, the "Configure for" dropdown defaults to Windows Vista for some reason so you might have look at that.

All mine run bat files, as mentioned above.
 
Just a rough thought. Have you tried to create a bat file / powershell equivalent. Then run the bat file with task scheduler with highest privileges ie Admin.

Created a bat file that runs the script - doesn't work, even when I run the bat file manually.
This makes me think it's not task scheduler causing the issue but rather the script that doesn't like being run from an "outside" command?

Start-up script no bueno ?


I'm using task scheduler because I need a delay after log-on to allow the network to be connected before trying to reconnect the mapped drives.
I'm a powershell script noob but I'm sure there's a way of putting that delay in the script. I'll look into it.

My task scheduler actions freak out when the "Run whether user is logged on or not" is ticked, even when I'm logged in, so might be that.
On windows10, the "Configure for" dropdown defaults to Windows Vista for some reason so you might have look at that.

All mine run bat files, as mentioned above.

My task is set to run only when user is logged on.
There is some weirdness nonetheless though - my task scheduler claimed that the scheduler service was unavailable if I had the "Start only if the following network connection is available" option ticked.
 
Created a bat file that runs the script - doesn't work, even when I run the bat file manually.
This makes me think it's not task scheduler causing the issue but rather the script that doesn't like being run from an "outside" command?
/snip

Get this working, and the scheduler should be fine.
 
Sure, but do you know how? The script works perfectly if I just right click -> run.
You can delay the task by a specified time.

Create task (Not the basic one),
Check the Run with highest Privilages box

When on trigger specify on logon/startup whichever you need, and add a 5-10 minute delay
 
Last edited:
Ok so two key things solved this for me:

1. Running the shell script from a batch file in Startup, NOT task scheduler. Don't know why task scheduler didn't work but it just doesn't. I included a "timeout 30" in my batch file to make sure the network connection is up before running the script.

2. Including the "-ExecutionPolicy ByPass" parameter when running powershell.exe from the batch file. This gets around the "scripts are not allowed to be run on this system" issue without permanently disabling the security measure.

Thanks for the answers everyone - I'll pick one to mark this question as answered.
 
Ok so two key things solved this for me:

1. Running the shell script from a batch file in Startup, NOT task scheduler. Don't know why task scheduler didn't work but it just doesn't. I included a "timeout 30" in my batch file to make sure the network connection is up before running the script.

2. Including the "-ExecutionPolicy ByPass" parameter when running powershell.exe from the batch file. This gets around the "scripts are not allowed to be run on this system" issue without permanently disabling the security measure.

Thanks for the answers everyone - I'll pick one to mark this question as answered.
Timeout is a bit iffy... A polling routing would work better there. But regardless, you couldn't manage to execute the script through scheduler. Glad you got it sorted out bud.

Cron ftw.
 

Users who are viewing this thread

Latest posts

Back
Top Bottom