New Scheduled Job

This topic contains 7 replies, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 4 months, 1 week ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #39233
    Profile photo of Mark C
    Mark C
    Participant

    Spent quite a bit of time on this, but coming up short. Basically, I'm trying to create a scheduled job to start a powershell script. The action that is "start a program" and the details are "Powershell.exe -ExecutionPolicy Bypass c:\scripts\misc\5-17\survey.ps1 -RunType $true -Path c:\scripts\misc\5-17". When I create the job manually via the gui and input these values it works fine when I run it, but I can get the powershell command below to create the task that will run properly.

    Here's the code that successfully creates the job

    Register-ScheduledJob -Name "ps11" -FilePath "c:\5_17\survey.ps1" -Trigger @{Frequency="Daily"; At="11:30PM"}

    But it creates this string in details

    -NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; $jobDef = [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore('ps11', 'C:\Users\sa445826\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs'); $jobDef.Run()"

    #39235
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So, for clarity, are you trying to create a scheduled task, or a scheduled job? The latter is a powershell only thing, and while it uses Task Scheduler, it is not the same as a scheduled task. There's also no GUI for creating a scheduled job, which is why I'm checking.

    #39239
    Profile photo of Mark C
    Mark C
    Participant

    hey Don. here's the code that i run, so it's a scheduledjob (in windows/powershell/scheduledjobs). I looked into creating a scheduled task, but the cmdlt was failing (I'm on windows 7).

    Register-ScheduledJob -Name ps13 -ScriptBlock {Powershell.exe -ExecutionPolicy Bypass c:\scripts\misc\5-17\survey.ps1 -RunType $true -Path c:\scripts\misc\5-17} -Trigger @{Frequency="Daily"; At="11:30PM"}

    A job is created, but nothing takes place when I run it. Now, the script is interactive (loads a menu for the user), so it may have something to do with setting it to interactive (looking at that now) as I see -Noninteractive set as detail.

    #39240
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Jobs are very different than a scheduled task. They definitely can't do interactive. And, any results they produce are stored on disk and must be retrieved by using Receive-Job. Scheduled jobs aren't a substitute for Scheduled Tasks – they have a unique purpose and way of executing.

    It seems like you're kind of hoping they'll work like a normal scheduled task and just run your script – but they won't :).

    #39245
    Profile photo of Mark C
    Mark C
    Participant

    Thanks Don. I've yet to find a way to create scheduled tasks with Powershell (New-ScheduledTaskAction not available in Windows 7). I can manually create a "Task" in the ScheduledJobs folder that correctly launches the script I'm working with and I can also edit a task/job I create with the Register-ScheduledJob cmdlt, so the job I am creating (after being edited) does launch the script properly. I would love to use scheduled tasks, but that isn't available as far as I can tell for Windows 7. Per your earlier comment around not being able to edit jobs, when I create a job via Register-ScheduledJob it appears in the task scheduler GUI and is able to be edited via the GUI. Not sure if we are talking about different things?

    #39246
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Use SchTasks.exe on Windows 7.

    A scheduled JOB does in fact create a scheduled TASK, but the TASK merely launches the PowerShell job subsystem. If you don't want to be using the job subsystem, you should not be using the ScheduledJob commands – they aren't interchangeable with simpler tasks.

    The reason the details of the task look weird to you is because that's what the task has to look like in order to invoke the job subsystem.

    See https://technet.microsoft.com/en-us/library/hh847802.aspx.

    #39248
    Profile photo of Mark C
    Mark C
    Participant

    Thanks Don. Never used SchTasks.exe before. What interface would I use? Powershell, command line? This seems to be functional so far in doing what I need – http://www.verboon.info/2013/12/powershell-creating-scheduled-tasks-with-powershell-version-3/. Trying to adjust running user information though...

    #39249
    Profile photo of Don Jones
    Don Jones
    Keymaster

    SchTasks is a command-line utility. You can run it from Cmd.exe or from PowerShell.exe. Either.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.