none
Creating a Scheduled Task using PowerShell v4 RRS feed

  • Question

  • So here is my question.  I'm trying/wanting to create a scheduled task using PowerShell v4 on Server 2012R2.  I want to task to run on the 1st day of every month.  The parameter for -Monthly -Day is not available.  I need to use PowerShell to create the Scheduled Task because the Task will be running with a group Managed Service Account credentials so the Scheduled Task can not be created via the Task Scheduler GUI.

    Thanks in Advance

    • Moved by Bill_Stewart Thursday, April 24, 2014 6:31 PM Move to more appropriate forum
    Thursday, April 24, 2014 6:14 PM

Answers

All replies

  • If you cannot do it in the Task Scheduler you cannot do it in PowerShell.

    I am sure none of us understand what you are asking.  What is a "Group Managed Account credential"? Do you mean a service account managed by Group Policy? 


    ¯\_(ツ)_/¯

    Thursday, April 24, 2014 11:54 PM
  • This one may help or maybe just use schtasks.exe with an XML config file.

    http://blogs.technet.com/b/askpfeplat/archive/2012/12/17/windows-server-2012-group-managed-service-accounts.aspx

     

     

     


    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows]

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.


    • Edited by Dave PatrickMVP Friday, April 25, 2014 12:52 AM
    • Proposed as answer by jrv Friday, April 25, 2014 1:36 AM
    • Marked as answer by Justin GuModerator Thursday, May 8, 2014 4:56 PM
    Friday, April 25, 2014 12:52 AM
  • Aha!  2012 has a new kind of MSA.  It is very useful.


    ¯\_(ツ)_/¯

    Friday, April 25, 2014 1:34 AM
  • Dave - Thanks for the reply and the Blog link. But,  I have all of that working.  What I want to do though is create a scheduled task that executes on a specific day of the month e,g the 1st, 10th, or 15th of each month.  This functionality is possible via the Task Scheduler GUI.  But you can't use the GUI if you are creating scheduled task that will run with the credentials of a gMSA.  The creation of the scheduled task must be done with PowerShell.  But PowerShell v4 does not support the -Monthly or -Day parameter.  Listed below is the syntax I'm currently using. It works great except I can't create a job for a specific day of the month:

    PoSh Code:

    $action =New-ScheduledTaskAction "C:\scripts\somescript"

    $settings = New-ScheduledTaskSettingsSet -Compatibility Win8

    $trigger = New-ScheduledTaskTrigger -At 08:00AM -Weekly -DaysOfWeek Friday

    $principal = New-ScheduledTaskPrincipal -RunLevel Highest -UserId domain\gmsa-account$ -LogonType Password

    Register -ScheduledTask "TaskName" -Description "Task Description" -Action $action -Settings $settings -Trigger $trigger -Principal $principal


    Thanks

    Friday, April 25, 2014 6:30 PM
  • PowerShell task triggers do not support monthly settings.  Use XML to define the task as suggested earlier.


    ¯\_(ツ)_/¯

    Friday, April 25, 2014 9:44 PM
  • Here is the section you have to replace:

    <Triggers>
        <CalendarTrigger>
            <StartBoundary>2014-04-25T17:31:46.7086967</StartBoundary>
            <Enabled>true</Enabled>
            <ScheduleByMonth>
                <DaysOfMonth>
                    <Day>1</Day>
                    <Day>15</Day>
                    <Day>22</Day>
                </DaysOfMonth>
                <Months>
                    <January />
                    <February />
                    <March />
                    <April />
                    <May />
                    <June />
                    <July />
                    <August />
                    <September />
                    <October />
                    <November />
                    <December />
                </Months>
            </ScheduleByMonth>
        </CalendarTrigger>
    </Triggers>
    

    Create a task for a day and export to XML> Edit the XML triggers and save tyhen re-import and you wil have what youa re looking for.

    Here is how to export and import with XML.

    http://technet.microsoft.com/en-us/library/cc709661.aspx


    ¯\_(ツ)_/¯

    Friday, April 25, 2014 9:51 PM
  • Hi,

    Would you please let us know current situation of this issue? Just check if jrv's suggestion can help you to solve it. If any update, please feel free to let us know.

    Best regards,

    Justin Gu

    Monday, April 28, 2014 12:45 PM
    Moderator
  • Sorry for the delay in responding - was on business travel and attended TechEd NA

    The Proposed solution did not work.  What I get is:

    "The format of the task is not valid.  The following error was reported: (22,6):Auhust:"

    Thanks

    Wednesday, June 4, 2014 9:31 PM
  • So create the task as above with your successful PowerShell and then export it to XML using jrv's link and post so we can look at it.

     

     

     


    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows]

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.

    Thursday, June 5, 2014 1:19 AM
  • Here is the command to export the XML:

    schtasks /query /TN "/Power Management" /xml one >task.xml

    The Taskname (TN) is the full path to the task. "/xml one" creates a single complete XML file.


    ¯\_(ツ)_/¯

    Thursday, June 5, 2014 1:29 AM
  • Here is a functioning task It can be easily edited in PowerShell or in notepad to alter the timing

    <?xml version="1.0" encoding="UTF-16"?>
    <Task version="1.1" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
      <RegistrationInfo>
        <Author>W8TEST\user01</Author>
      </RegistrationInfo>
      <Triggers>
        <CalendarTrigger>
          <StartBoundary>2014-06-04T21:31:32.0459499</StartBoundary>
          <Enabled>true</Enabled>
          <ScheduleByMonth>
            <DaysOfMonth>
              <Day>1</Day>
              <Day>12</Day>
              <Day>24</Day>
            </DaysOfMonth>
            <Months>
              <January />
              <February />
              <March />
              <April />
              <May />
              <June />
              <July />
              <August /
              <September />
              <October />
              <November />
              <December />
            </Months>
          </ScheduleByMonth>
        </CalendarTrigger>
      </Triggers>
      <Principals>
        <Principal id="Author">
          <RunLevel>LeastPrivilege</RunLevel>
          <UserId>W8TEST\jvierra</UserId>
          <LogonType>InteractiveToken</LogonType>
        </Principal>
      </Principals>
      <Settings>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
        <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
        <IdleSettings>
          <StopOnIdleEnd>true</StopOnIdleEnd>
          <RestartOnIdle>false</RestartOnIdle>
        </IdleSettings>
        <Enabled>true</Enabled>
        <Hidden>false</Hidden>
        <RunOnlyIfIdle>false</RunOnlyIfIdle>
        <WakeToRun>false</WakeToRun>
        <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
        <Priority>7</Priority>
      </Settings>
      <Actions Context="Author">
        <Exec>
          <Command>notepad.exe</Command>
          <Arguments>test.txt</Arguments>
          <WorkingDirectory>c:\temp</WorkingDirectory>
        </Exec>
      </Actions>
    </Task>
    

    I have edited and reloaded the XML many, many times.  It works very nicely.


    ¯\_(ツ)_/¯

    Thursday, June 5, 2014 1:38 AM
  • Here is the command to load the task:
    schtasks /create /xml c:\test\mytask.xml


    ¯\_(ツ)_/¯

    Thursday, June 5, 2014 1:40 AM
  • JRV - Thanks for the .xml code.  It really does me no good (sort of).  If I were using a regular service account to run the scheduled task then importing the xml to a task would work just fine.  But as I have stated before I'm utilizing Group Manager Service Accounts (gMSA) and since I have know idea what the PW for the gMSA account is I have no way of passing the task the needed password.  So although the scheduled task will successfully create it will not run do to logon failure.

    Thanks

    Wednesday, June 11, 2014 5:53 PM
  • With service accounts you do not supply a password.  The password is not kept locally.  You must be an admin with permission to manage the service account and task scheduler will do the rest for you.

    Sorry.  This is how it works.  I cannot teach you how to use these things so you will have to try to get it to work by following the instructions.  There is not other way to do what you are trying to do short of writing a custom program that calls the low level API.

    IF you cannot do it with XML imported than you are out of luck.

    Of course you need to know that you must crate the task in TS first then you can edit the XML whenever you need to change things.  The XML must be created by exporting the task.  You cannot easily just copy XML from one system to another unless you know how all of this works which it is clear that you don't.

    Try doing what I posted originally.  I have done this dozens of times and have never had an issue.


    ¯\_(ツ)_/¯

    Wednesday, June 11, 2014 6:31 PM
  • JRV - I'm not asking anyone to teach me how to use these thing.  I have many Scheduled Tasks running with both traditional services accounts and gMSA's and created via traditional means and PowerShell.  All of which work just fine.  The issue is PowerShell v.4 not supporting -DayOfMonth entries.

    Thanks

        
    Wednesday, June 11, 2014 7:13 PM
  • PowerShell only supports tasks as much as is needed for the Jobs scheduler.  It iws not a substitute for the task scheduler API.

    The XML export can be edited to alter the schedule as needed then imported back into the task scheduler.  It woks and allows us to do all of thise many things that PowerShell cannot do.


    ¯\_(ツ)_/¯

    Wednesday, June 11, 2014 7:21 PM
  • Please read the following:

    4. Using the gMSA for a Scheduled Task

    Since my use case was a scheduled task, I’ll show you some of the interesting nuances around gMSA and scheduled tasks.  The fundamental problem is that you can’t use the Task Scheduler UI.  So we’ll use PowerShell cmdlets, instead. (You could also use schtasks.exe with an XML config file, but I’ll let you figure that one out yourself).

    To use the PowerShell cmdlets, you need to define an Action (what), a Trigger(when) and a Principal(under which identity):

    $action = New-ScheduledTaskAction  "c:\scripts\backup.cmd"
    $trigger = New-ScheduledTaskTrigger -At 23:00 -Daily
    $principal = New-ScheduledTaskPrincipal -UserID child\myAdminAccount$ -LogonType Password

    After the –LogonType switch you type the word Password, and not an actual password.  This tells the scheduled task to retrieve the actual password for the gMSA from a domain controller.


    ¯\_(ツ)_/¯

    Wednesday, June 11, 2014 7:26 PM
  • This may help to explain a way to do this:

    http://blog.simonw.se/using_standalone_managed_service_accounts_for_scheduled_tasks/


    ¯\_(ツ)_/¯

    Wednesday, June 11, 2014 7:37 PM
  • Instead of using the New-ScheduledTaskTrigger cmdlet, use this method to build your trigger variable :

    $triggers = $TaskDefinition.Triggers

    $trigger = $triggers.Create(4)
    $
    trigger.DaysOfMonth = 1

    ASK_TRIGGER_EVENT      0
    TASK_TRIGGER_TIME     1
    TASK_TRIGGER_DAILY      2
    TASK_TRIGGER_WEEKLY     3
    TASK_TRIGGER_MONTHLY      4
    TASK_TRIGGER_MONTHLYDOW     5
    TASK_TRIGGER_IDLE     6
    TASK_TRIGGER_REGISTRATION     7
    TASK_TRIGGER_BOOT     8
    TASK_TRIGGER_LOGON      9
    TASK_TRIGGER_SESSION_STATE_CHANGE     11

    The full explanation is here :

    http://www.verboon.info/2013/12/powershell-creating-scheduled-tasks-with-powershell-version-3/

    Monday, July 4, 2016 9:57 AM
  • Winner Winner!  Create the task via any allowed account, with whatever schedule in the GUI, then run the schtasks command from the above link on it, and it will change the user to the gMSA account!

    schtasks /Change /TN "Folder\Task Name" /RU "Domain\gMSASchedTask$" /RP ""

    w00t!

    Thursday, February 22, 2018 1:07 AM
  • Dan, I am in the same boat.  I need a GMSA task to run the 1st of the month.  I tried your method of using schtasks /change and it does not work.  For some reason it breaks the job and tells me "file cannot be found".

    If I use "Register-ScheduledTask" to create the job for something like 6 AM daily, and then manually run that task - it works.  I then edit the job, change the trigger to monthly, set it run as my account and save it...then use your schtasks /change technique - it does make the change.  I see the GMSA account as run account.  However when I manually run the job, it tells me the file cannot be found.  Not sure what "file".  Its a powershell script I am running, so I am assuming the PS1 file...

    Any ideas?

    NK

    Wednesday, March 28, 2018 3:17 PM
  • I think the /RP "" is messing up the GSMA password and breaking the task...although I still don't get why the file not found error message.

    What I did for now was set the task to run daily using the Register-ScheduledTask cmdlet so that the GMSA password is set properly, and then added "if ((get-date).Day -eq 1)" to my PS script.  Not the best solution, since the task itself runs daily instead of once a month.

    NK

    Wednesday, March 28, 2018 3:46 PM