Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

scheduling powershell script

Posted on 2014-01-27
4
Medium Priority
?
687 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 2000 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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
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.

618 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