I am trying to create a scheduled task from Powershell.
I was readin the document for cmdlet: New-ScheduledJobOption.
And I am a bit confused...
Question:
Are 'Scheduled Job" and "Scheduled Task" the same thing in Powershell context? Or there is a difference?
So just to put an actual answer out there, in addition to the article Valamus put out:
In a PowerShell context Scheduled Jobs and Scheduled Tasks are not the same thing. From a task scheduler perspective they are exactly the same thing.
The PowerShell team provided a toolset for creating a special type of job that would be stored in the Task scheduler library. The Scheduled Jobs cmdlets will not allow you to interact with regular scheduled tasks. The Task Scheduler will let you interact with regular tasks and scheduled jobs interchangeably.
Scheduled Task is just a Windows thing and PowerShell can control it in the same way it (PowerShell) can control many other Windows things.
Scheduled Job is, in the other hand, mostly PowerShell thing. Namely, scheduled job is some PowerShell code scheduled for execution in a "Windows Task Scheduler" manner and executed within PowerShell environment (naturally).
So, an activity run as a Scheduled Task is anything you can run from command prompt (eg. .EXE, .BAT, .COM, .CMD etc. - nothing PowerShell-ish, unless you decide to start powershell.exe and give it some script name to execute. Yet, you can control all of that from PowerShell.
On the other hand, Scheduled Job is exclusively PowerShell code (be it script block or script file) that PowerShell registers with Windows Task Scheduler, and Windows Task Scheduler knows how to activate it. Method of repetitive activation is in both cases the same ("triggers").
So, Powershell Scheduled Task is a Windows thing (controllable also from PowerShell), while PowerShell Scheduled Job is a exclusively PowerShell thing.
Related
I've created a PowerShell script which lists the scheduled tasks and the LastRunResult. I'm using Get-ScheduledTask command. I need to put it at the end of every scheduled task to send a notification related to the LastRunResult.
The problem is that Get-ScheduledTask command doesn't work properly and do not show ALL scheduled tasks but just some of them. To be able to see all scheduled tasks the Get-ScheduledTask command needs to be run from the CMD or PowerShell console with administrative privileges. And it works from there. But it doesn't work when I run such script from Task Scheduler. Checking Run with highest privileges doesn't work. Specifying Policy -Bypass doesn't work as well.
Does anyone know how to execute Get-ScheduledTask from Task Scheduler ran in administrator mode to list all scheduled tasks?
This can be caused if you have UAC enabled or are logged on as a user who is not an administrator.
Ensure the runas account is a local administrator.
On a Windows 2012 R2 server there is a Powershell script that I can manually invoke to start a process on some EXE, this works just fine.
But when trying to trigger it via a scheduled task (invoking the same script) the start-process within the script just doesn't trigger or finish. Causing the task scheduler to terminate the task due to exceeding the timeout threshold.
Here's the core section of the script:
$exe = "c:\some\app.exe"
$arguments = "-user me -pwd secret"
$process = Start-Process $exe -ArgumentList $arguments -PassThru -Wait
return $process
Is there some way I can get some insights into what start-process is doing or why the same script works when invoked manually but not programmatically?
I want to emphasize that the way the script is invoked from the scheduled task is not a problem! The script triggers because the corresponding log file populates.
Any insights or help on this is greatly appreciated!
quick update on this since I found the problem. It turns out, it had nothing to do with either the powershell script or the scheduled task itself...
On the machine the script is running on, there is a network share that is mapped as the z:\ drive. I use it to save logs to. Now apparently that mapping/mounting is handled differently depending on whether the script is invoked interactively or programatically, because in the latter case it appears that the resoultion of the network path \\network\share\folder1 does not succeed, however there is nothing complaining about it, the process just silently does not start. If however, I point the logs to a physical local path or the explicit full network path itself, there is no problem running the script.
Lesson learned, never trust OS' drive mapping of network paths :D
Cheers
I developed a console app and scheduled it to run on Mon-Friday at 9:AM using windows task scheduler, it works fine.
But I was asked to write Powershell scripts or command-line scripts for deployment, like the script should copy the "Release" folder to the server1(deployment env like dev, test or staging or prod) and schedule it in server1's schedule task. Also I have to set "Start in(optional)".
I don't know anything about command-line and PS scripts. All it should do is create the windows schedule task just like the way I did using UI.
If I understand the question correctly you're just trying to create a scheduled task using powershell?
If so try starting here:
https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/13/use-powershell-to-create-scheduled-tasks/
I had a requirement to schedule an exe, this scheduled task should run every day Mon to Fri at desired time. I never wrote any batch files in last 15 years of my career :), took it up as it came in my way from my manager. Thanks to him or else I would have not known in this new world.
echo off
title My 1st batch file for Task scheduler
SchTasks /Create /RU "system" /Sc WEEKLY /D MON,TUE,WED,THU,FRI /TN "Leads" /TR "C:/Some.exe" /ST 09:00
I've scheduled a batch file that has this command in a TaskScheduler and it never completes:
powershell -command "& 'C:\Test\CleanUp.ps1'"
The .ps1 file has a simple script that deletes files on different shares. The powershell script runs successfully when run in ISE or even the above mentioned command runs successfully when run at cmd prompt.
I've even tried the option below with the command and also searched a number of blogs but no luck.
-ExecutionPolicy Unrestricted
The task is scheduled to run with a service account and I've setup a number of identical jobs the same way and they worked fine. I have powershell version 4.0.
How do I get the job to complete? (successfully or with a failure)
I have seen this problem before and generally it turns out to be that, when running as a different user and/or with a different profile, the script hangs because it needs to prompt for input, e.g. a missing mandatory parameter, Get-Credential, or something like that.
I'll assume that you have run the command from an interactive shell running as the service account, as this could reveal whether there is something different about the service account profile, etc. that may interfere with the running of the script.
I'll also assume that you have tried setting up the scheduled task with your own account, under which you know the script will run correctly.
Next, look at some of the other command line options for powershell.exe:
https://technet.microsoft.com/en-gb/library/hh847736.aspx
I'm not sure whether you'll get much mileage from the -ExecutionPolicy switch, as my experience is that people tend to set the policy to Unrestricted almost as soon as the server is commissioned and then never think about it again.
You might want to look at the -NoProfile switch, as that is something I see used quite often when calling PowerShell from a .bat file or directly in the task scheduler.
Notice the -File switch, which you might consider using instead of -Command.
I have seen various articles on how to schedule a Powershell script, but I have not see much of the reverse. I need to call a manual task in Task Scheduler from my PowerShell script. I am using Powershell 2.0. Can anyone show me how to do this? Thanks.
The simplest way may be to use schtasks.exe
& schtasks.exe /Run /TN <Name of Scheduled Task>
NOTE -- on my Windows 8.1 preview with PowerShell 4.0 there is a Start-ScheduledTask cmdlet.