Solved

scheduling powershell script

Posted on 2014-01-27
4
677 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 29

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 29

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this Micro Tutorial viewers will learn how to restore their server from Bare Metal Backup image created with Windows Server Backup feature. As an example Windows 2012R2 is used.
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…

732 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