Solved

scheduling powershell script

Posted on 2014-01-27
4
664 Views
Last Modified: 2014-01-27
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
Comment
Question by:crcsupport
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
ID: 39812875
For your scheduled task, run with an account that has admin privileges.

Then in your script for the firs line:
set-Executionpolicy Bypass
0
 
LVL 1

Author Comment

by:crcsupport
ID: 39812886
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
 
LVL 1

Author Comment

by:crcsupport
ID: 39812911
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
 
LVL 28

Expert Comment

by:becraig
ID: 39812922
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What to do when Windows Update is not working correctly? What tools can I use to detect the cause of the malfunction problem? What does this numeric error code mean? These and other questions that you have been asking in the past are answered here (…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
In this Micro Tutorial viewers will learn how to use Windows Server Backup to create full image of their system. Tutorial shows how to install Windows Server Backup Feature on Windows 2012R2 and how to configure scheduled Bare Metal Recovery backup.…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now