• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 697
  • Last Modified:

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
0
crcsupport
Asked:
crcsupport
  • 2
  • 2
1 Solution
 
becraigCommented:
For your scheduled task, run with an account that has admin privileges.

Then in your script for the firs line:
set-Executionpolicy Bypass
0
 
crcsupportAuthor Commented:
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
0
 
crcsupportAuthor Commented:
Solved the problem -ExecutionPolicy ByPass has to come first in argument field in task scheduler setting. It seems as the argument is processed serially
0
 
becraigCommented:
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.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now