crcsupport
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
I scheduled this;
powershell.exe -Command c:\scripts\deleteoldfiles.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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