Link to home
Start Free TrialLog in
Avatar of crcsupport
crcsupportFlag for United States of America

asked on

scheduling powershell script

I'm trying to schedule a task with a powershell script.
I scheduled this;
powershell.exe -Command c:\scripts\deleteoldfiles.ps1

It works. However, I don't like to make execution policy for local machine to unrestricted. So I did this;
Set-ExecutionPolicy Unrestricted
Then I changed scheduled task to;
powershell.exe -Command c:\scripts\deleteoldfiles.ps1 -ExecutionPolicy ByPass

This doesn't work. I selected 'run with highest privileges' in the task, unblocked the script, still doesn't work.

What's wrong?

Windows 2012 server
powershell 3.0
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America 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
Avatar of crcsupport

ASKER

I'm running with admin account.
I'm running the script as task in restricted execution policy. If I put it inside script, won't it dismiss anyway? I think it has to be set in task schedule as argument to powershell.exe
Solved the problem -ExecutionPolicy ByPass has to come first in argument field in task scheduler setting. It seems as the argument is processed serially
My bad :(

You should be able to add -ExecutionPolicy Bypass to the powershell command the task runs.

I have in the past used  :
-ExecutionPolicy Bypass  -NoProfile –NonInteractive for my scheduled tasks on restricted systems and have had no issues.