Edit triggers for a Windows Task Scheduler Task - powershell

We currently have several hundred tasks scheduled in Windows Task Scheduler on our server 2008 box. There are a number of tasks that are running every half hour indefinitely. Is there any way to use a script to change the triggers on those tasks so that they will only run every half hour M-F?

Yes, if you use the TaskService COM object. For some example scripts that use this object, see the following:
Rename Scheduled Tasks in Windows 7, Windows Server 2008, and Windows Vista
How-To: Use PowerShell to Report on Scheduled Tasks
Updating a Scheduled Task's Credentials
Script: https://gist.github.com/Bill-Stewart/363fdf761499a071439c8ac0c18dcd54
Using the schtasks.exe utility is rather limited past Windows XP/Server 2003.

Here are the allowed options for /SC:
/SC allows for the following frequency schedules:
MINUTE: 1 - 1439 minutes.
HOURLY: 1 - 23 hours.
DAILY: 1 - 365 days.
WEEKLY: weeks 1 - 52. (/D MON,TUE,WED,THU,FRI,SAT,SUN)
ONCE: No modifiers.
ONSTART: No modifiers.
ONLOGON: No modifiers.
ONIDLE: No modifiers.
MONTHLY: 1 - 12, or FIRST, SECOND, THIRD, FOURTH, LAST, LASTDAY.**
I keep editing this because I'm not happy with my findings...
I have looked into the SCHTASKS and noticed that you cannot CHANGE for the schedule through CMD.
From technet.microsoft
Changes one or more of the following properties of a task.
The program that the task runs (/tr).
The user account under which the task runs (/ru).
The password for the user account (/rp).
I am going to continue to do some research on this, and hopefully will edit this answer with a workable solution.
EDIT
If you look in C:\windows\system32\tasks you will find all of your scheduled tasks
They're not listed as XML, but they are indeed XML
What I think should be done is using either; VBS or C# application dig to each of the following Nodes
//Task/Triggers/CalendarTrigger to delete /ScheduleByDay and it's child node /DaysInterval
//Task/Triggers/CalendarTrigger to create the following:
<?xml version="1.0" encoding="UTF-16"?>
-<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task" version="1.2">
+<RegistrationInfo>
-<Triggers>
-<CalendarTrigger>
<StartBoundary>2013-03-13T15:20:00</StartBoundary>
<Enabled>true</Enabled>
-<ScheduleByWeek>
-<DaysOfWeek>
<Monday/>
<Tuesday/>
<Wednesday/>
<Thursday/>
<Friday/>
</DaysOfWeek>
<WeeksInterval>1</WeeksInterval>
</ScheduleByWeek>
</CalendarTrigger>
</Triggers>
If anyone can pick up and run with this - I don't think I'll be able to do much with this in the next week.

I found that if you use SCHTASKS /Create and use the same /TN name with a different /SC and /ST options it will overwrite the existing scheduled task with the updated schedule

Related

Can a PowerShell script be dependent of another script's execution?

I have a situation where I want to make the execution of my scripts smarter. I have a set of scripts that execute at a given time, but because sometimes the input files are not posted at the correct times the scripts run into errors and get unexpected results. so one of the solutions I was thinking of is to make the execution of the scripts dependent of each other. Here is what I mean:
script 1 runs at 6 pm
validates that the file is there
if it's there, set a flag
the flag is active so execute script 2 at 9 pm
if it's NOT there, the flag is not set
the flag is not set so script 2 is not executed
Right now script 1 and script 2 are set with the Task Scheduler at those times, I checked the Scheduler for those type of conditions, but didn't find anything.
You can set triggers in Task Scheduler, like when an event happens for basically everything you can see in eventviewer.
I would suggest Write-Eventlog from the script which works on the file, and depending on the result the sched task would get triggerd.
I suggest you to have single script running every N-minutes on single scheduled task via Task Scheduler.
The master script will analyze activities and have all logical conditions those determine when and which external script to run. You can also have flag files.

How to run an Event (FileSystemWatcher) through Task Scheduler

I currently have a pretty simple Powershell Script that creates an IO.FileSystemWatcher object, and calls an executable upon that event being triggered.
I can run this script without issue from Administrator Powershell on my 2012 Windows Server, however it seems to run into issues when I have my script being run from Task Scheduler.
I've attempted running the task while logged on, and on a trigger while I'm logged off and in both instances the Event status reads: "Running" when I check. However interacting with the folder that should be watched produces no results. I've added a log file to document which parts of the code are functioning and the script DOES create the event, however it is the event triggering that seems to be the issue. Has anyone heard of an issue with creating events through Task Scheduler?
I've read some forums that say it might be a domain user issue
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa
Change the ‘REG_DWORD’ with ValueName ‘disabledomaincreds’ to a Value to “0
Although this was already the case, and I've tried multiple variations of settings in the Task Properties as per Scripting Guy and SpiceWorks. The general consensus I've found is that it needs to be ran with a -NoExit argument in order for the event to properly run when the user is not logged in.
Extra notes:
Powershell script is located on a network location rather than physically on the computer (\serverName\FTP\Folder\script.ps1
I came across the same problem. I don't know why this works, but in your Scheduled Task, when referring to the PowerShell Script, instead of using
\serverName\FTP\Folder\script.ps1
use
. \serverName\FTP\Folder\script.ps1
(noting the .).
As I understand, as a powershell novice, the events you register with FileSystemWatcher will only fire if the powershell instance is still running. I wouldn't trust that task manager says the task is running since it is notoriously unreliable, which seems to be the Microsoft standard. I think once your script finishes executing it kills the powershell instance and all event listeners are garbage collected.
I just put my script to sleep forever and it works. At the end of my script, it has
while ($true) {sleep 1}
It probably wouldn't hurt to increase the sleep time, but this works.

Scheduled task not created with silent installation

I've created a setup file using InstallShield Limited Edition, and I have a custom action that creates a scheduled task in the "After Setup Completed Success dialog" custom actions folder for installation:
schtasks.exe /create /xml "[INSTALLDIR]\task definition.xml" /tn "My Task"
If I run the setup as administrator, or I start it from a system context, everything goes fine.
However, if I run it silently, with the /S /v/qn command line parameters, everything works except for the task, which is not created.
How can I create the task also with a silent installation?
Lots of reason this could happen. If for example you have this running as a custom action in your UI sequence, the UI sequence doesn't run under a silent install. You would need to put it in the Execute sequence.

How do schedule a task in 1 hour using command prompt

I need some help setting up a batch file in Windows 7. I want the batch file to be able to create a scheduled task that would execute 1 hour from the moment I click it. I don't want to manually have to put the date and time in, just want it to schedule a task that will execute a set number of hours after I have run the batch file (I'm using 1 as an example).
Please can somebody help me out. I've been searching for an answer all day to no avail.
Recent versions of Windows come with a DOS utility called WAITFOR. Depending on how interactive you want your batch file, and whether it should run a single static command or run whatever you need at the time, you could easily make it work. Like for instance, you could create a batch file on your desktop and drag a program to it and drop it on the batch file. The first thing it would do is prompt for the number of minutes to delay, then it could run the program you dropped on it.
#echo off
setlocal enabledelayedexpansion
set /p _min=Enter the minutes to delay:
set /a _min*=60
waitfor /t !_min! delay
start "" %1
setlocal
Using the start command makes it possible to drop other things too, like a BMP or Word DOC. Anything that you can launch by double-clicking it from Windows Explorer should launch just fine.
After you enter the minutes to delay, just minimize the DOS window. It will close automatically after the delay and after it launches the program or file you dropped on the batch file.
invoke windows task scheduler directly from command line
schtasks /create /TN "Task Name" /TR script.bat /ST 18:00 /SD 21/03/2014 /SC ONCE

simple command prompt one line into exe

I have a perl script that I run from the command prompt: 3 times a day, the problem is now I need to run it every two hours. Im not going to be on an episode of lost so I need some help. Here is the command:
Perl C:/test/scripts/edi.pl
Does anyone know how this above one line command can be made into an executable (.exe) file so I can use the task scheduler to run?
If there is another way to do this with the task scheduler to run once every two hours every day then I could do that as well.
Thanks for your time.
Can you not simply create a batch file that runs the script, and set that batch file to run every two hours? I know from a quick test there that I can schedule a batch file to run from Task Scheduler on Windows XP at least.
You can actually use the task scheduler to run that exact command without a batch.
The task scheduler should allow you to pass some arguments to the script without a problem (I have done this on a few Windows servers in order to have them run PHP scripts)