Link to home
Start Free TrialLog in
Avatar of narce100
narce100

asked on

Scheduled Task to run PowerShell script

I’m trying to create a task in Task Scheduler that will run a PowerShell script at 12:00 am every day and it’s not working, here are my settings: (see attach).
I’m also including a copy of the script file in notepad. Can anyone help? Thanks.
User generated imageScript.txt
Avatar of Wayne88
Wayne88
Flag of Canada image

Does it work when you manually run the task?

Windows 7, Server 2008, etc. is very finicky with running scheduled task because we find that you cannot be logged off or the task won't run for example.  Some others claimed making the task to run as administrator and wake computer up automatically, etc. worked for them.

First and foremost, set the task to run as a user with administrator privileges.
You have the incorrect syntax in the Task scheduler to run this script. You cannot call a .PS1 script separately. You first need to call powershell. So it should look like below...

Details should look like below...
powershell.exe -file "c:\filepath\scriptname.ps1"

Once you have it set like above it should run successfully.

Will.
Avatar of narce100
narce100

ASKER

Wayne88: Yes I run the script manually every day for quite some time but now I need to run it late at night.

Will: I'll try that syntax and see if it works.
I wrote a shorter and easier version of the report.ps1 file, it ran well manually but when I set up the trigger to run a few minutes after my current time. It does not seem to work, but the log shows that it did. Attached is a print screen with the settings and a notepad copy of the file.
New-seetings.PNG
More-settings.PNG
Script.txt
Another thing you can try is just call the powershell script from a batch file.
http://stackoverflow.com/questions/19335004/how-to-run-a-powershell-script-from-a-batch-file

Will.
Will is correct on his post about using

powershell.exe -file "c:\filepath\scriptname.ps1"

Here is what it should look like.

User generated image
The task ran and was completed according to Task Scheduler history log; but I don't see the script run or the files that are supposed to generate are not on the designated folder see attach for settings and script file
History-log.PNG
Action-Settings.PNG
PowerShell-Script.txt
What account are you running the task under? If the S Drive is not a local drive and it's actually a network drive then you will need to mount S: within the script as well
I already have a map on my desktop to the S: drive and when creating the Task I sign in as myself (Domain Administrator) and the logon window disappears after that.
It runs fine when I do it manually; do I have to write the entire path instead of the mapping on the script?
Yes I would. Including the entire network path will reduce the points of failure. Did you put in your credentials so that the task will run using your active directory account?
Yes, actually a window comes up every time you modify something on the task scheduler and prompts you to type username/password under the task will run on.
I will write the entire path and keep you posted on the results
I typed the entire path and the task since to run with no problem is the PowerShell script that does not run, I don't see it come up or anything.
new-task-scheduler-screeshot.PNG
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It worked, I have some fine tune to do in the script but is fine.

Thank You.