none
Powershell: create scheduled task which repeats every other month? RRS feed

  • Question

  • Hi,

    I have a Powershell-script to deploy windows updates every other month on PatchTuesday.

    However I don't find a way to schedule it (via Powershell every other month). Please advise how this can be done.
     

    $TriggerSchedule = New-ScheduledTaskTrigger -At "$ScheduledDatePatchTuesday" .... every other month

    J.


    Jan Hoedt

    Monday, February 8, 2016 5:26 PM

Answers

  • This is the trick:

    <ScheduleByMonthDayOfWeek>
            <Weeks>
              <Week>1</Week>
            </Weeks>
            <DaysOfWeek>
              <Tuesday />
            </DaysOfWeek>
            <Months>
              <January />
              <March />
              <May />
              <July />
              <September />
              <November />
            </Months>
          </ScheduleByMonthDayOfWeek>
    
    Add months and weekdays by name.


    \_(ツ)_/

    • Marked as answer by janhoedt Tuesday, February 9, 2016 5:22 PM
    Tuesday, February 9, 2016 1:08 AM
  • Right, since there are no "monthly" options in the New-ScheduledTaskTrigger cmdlet, you will have to use an alternative method to define your task trigger (such as the scheduler GUI, an XML file as noted, the schtasks command, etc.).

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by janhoedt Tuesday, February 9, 2016 5:22 PM
    Tuesday, February 9, 2016 4:46 PM
    Moderator

All replies

  • Use WSUS.

    -- Bill Stewart [Bill_Stewart]

    Monday, February 8, 2016 5:31 PM
    Moderator
  • Create 6 yearly triggers.

    Monday, February 8, 2016 5:36 PM
  • We use wsus in sccm.

    Jan Hoedt


    • Edited by janhoedt Monday, February 8, 2016 5:42 PM update
    Monday, February 8, 2016 5:41 PM
  • Thanks but no option for me. Dates should be dynamic, each month the script generates new scheduled jobs and I should be able to disable it not create all jobs at one ho.

    Jan Hoedt

    Monday, February 8, 2016 5:48 PM
  • This does not seem to be a scripting question.


    -- Bill Stewart [Bill_Stewart]

    Monday, February 8, 2016 6:12 PM
    Moderator
  • Why not? I use ps to deploy unpretentious to collections.

    Jan Hoedt


    • Edited by janhoedt Monday, February 8, 2016 6:35 PM update
    Monday, February 8, 2016 6:34 PM
  • Windows update and SCCM questions are outside the scope of this forum.


    -- Bill Stewart [Bill_Stewart]

    Monday, February 8, 2016 6:38 PM
    Moderator
  • Windows update and SCCM questions are outside the scope of this forum.


    -- Bill Stewart [Bill_Stewart]


    I m not asking anything about sccm, I m asking about creating a scheduled Job in powershell.

    Jan Hoedt

    Monday, February 8, 2016 6:44 PM
  • I recommend reading the documentation.

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


    -- Bill Stewart [Bill_Stewart]

    Monday, February 8, 2016 6:49 PM
    Moderator
  • That s  what I did before posting this. There is no option for what I want to do.

    Jan Hoedt

    Monday, February 8, 2016 7:58 PM
  • That s  what I did before posting this. There is no option for what I want to do.

    Jan Hoedt

    Hence my suggestion of 6 yearly triggers for the scheduled task.

    I have no idea what you mean by your previous comment though. You can create the dates however you'd like to.


    Monday, February 8, 2016 8:00 PM
  • Use XML:

    <?xml version="1.0" encoding="UTF-16"?>
    <Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
      <RegistrationInfo>
        <Date>2016-02-08T13:50:09.9164921</Date>
        <Author>ALPHA\JSMITH</Author>
        <Description>Used for testing and XML output</Description>
        <URI>\TEstTask</URI>
      </RegistrationInfo>
      <Triggers>
        <CalendarTrigger>
          <StartBoundary>2016-02-08T13:47:28</StartBoundary>
          <Enabled>true</Enabled>
          <ScheduleByMonthDayOfWeek>
            <Weeks>
              <Week>1</Week>
            </Weeks>
            <DaysOfWeek>
              <Tuesday />
            </DaysOfWeek>
            <Months>
              <January />
              <March />
              <May />
              <July />
              <September />
              <November />
            </Months>
          </ScheduleByMonthDayOfWeek>
        </CalendarTrigger>
      </Triggers>
      <Principals>
        <Principal id="Author">
          <UserId>S-1-5-21-355555555-3993594425-821974695-1001</UserId>
          <LogonType>Password</LogonType>
          <RunLevel>LeastPrivilege</RunLevel>
        </Principal>
      </Principals>
      <Settings>
        <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
        <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
        <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
        <AllowHardTerminate>true</AllowHardTerminate>
        <StartWhenAvailable>false</StartWhenAvailable>
        <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
        <IdleSettings>
          <StopOnIdleEnd>true</StopOnIdleEnd>
          <RestartOnIdle>false</RestartOnIdle>
        </IdleSettings>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <Enabled>true</Enabled>
        <Hidden>false</Hidden>
        <RunOnlyIfIdle>false</RunOnlyIfIdle>
        <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
        <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
        <WakeToRun>false</WakeToRun>
        <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
        <Priority>7</Priority>
      </Settings>
      <Actions Context="Author">
        <Exec>
          <Command>powershelll</Command>
          <Arguments>-file test.ps1</Arguments>
          <WorkingDirectory>c:\test</WorkingDirectory>
        </Exec>
      </Actions>
    </Task>
    Notice the monthly and daily settings.


    \_(ツ)_/

    Tuesday, February 9, 2016 1:07 AM
  • This is the trick:

    <ScheduleByMonthDayOfWeek>
            <Weeks>
              <Week>1</Week>
            </Weeks>
            <DaysOfWeek>
              <Tuesday />
            </DaysOfWeek>
            <Months>
              <January />
              <March />
              <May />
              <July />
              <September />
              <November />
            </Months>
          </ScheduleByMonthDayOfWeek>
    
    Add months and weekdays by name.


    \_(ツ)_/

    • Marked as answer by janhoedt Tuesday, February 9, 2016 5:22 PM
    Tuesday, February 9, 2016 1:08 AM
  • Thanks, but I'm not familiar with doing it via xml. Didn't see your last post yet and found another way (schtasks), however didn't find a way to run it every 2 months since /mo only accepts one argument (and it should take 2 and SECOND)

     schtasks /create /tn $MyScheduledTask /tr "powershell -noexit -command &{Deploy-SoftwareUpdates -Month $Month -Environment TEST -StartDate$StarteDate}" /sc monthly /mo SECOND /d WED /ST 06:00


    Jan Hoedt

    Tuesday, February 9, 2016 1:59 PM
  • Right, since there are no "monthly" options in the New-ScheduledTaskTrigger cmdlet, you will have to use an alternative method to define your task trigger (such as the scheduler GUI, an XML file as noted, the schtasks command, etc.).

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by janhoedt Tuesday, February 9, 2016 5:22 PM
    Tuesday, February 9, 2016 4:46 PM
    Moderator
  • Very easy:

    schtasks /create /xml MyTaskName.xml

    Create your task.  Export it as XML then edit the xml using [xml] object then use the above command to update the task.


    \_(ツ)_/


    • Edited by jrv Tuesday, February 9, 2016 5:39 PM
    Tuesday, February 9, 2016 5:38 PM
  • Cool, now how do you create a clustered scheduled task to run monthly?
    Tuesday, October 23, 2018 4:33 AM
  • Cool, now how do you create a clustered scheduled task to run monthly?

    Just specify all months.

    \_(ツ)_/

    Tuesday, October 23, 2018 4:43 AM
  • But you can only create (or modify) clustered scheduled tasks with powershell. You can't use schtasks or the gui.

    So you need to do something like;

    $TaskTrigger = New-ScheduledTaskTrigger -Some-Options
    Register-ClusteredScheduledTask -Trigger $TaskTrigger -MoreOptions

    So how can you define a monthly trigger with PowerShell?

    "It isn't possible" is an acceptable answer at this point :)

    Tuesday, October 23, 2018 4:47 AM
  • Why does that prevent you form using the XML as posted above?


    \_(ツ)_/

    Tuesday, October 23, 2018 4:52 AM
  • Ah gotcha, yes thank you that will work better than what I was trying to do anyway.
    Tuesday, October 23, 2018 4:56 AM