Parsing Scheduled task : Interval Element back to Time Span object type value

Welcome Forums General PowerShell Q&A Parsing Scheduled task : Interval Element back to Time Span object type value

  • This topic has 2 replies, 2 voices, and was last updated 1 week ago by
    Participant
    .
Viewing 2 reply threads
  • Author
    Posts
    • #191605
      Participant
      Topics: 3
      Replies: 5
      Points: 55
      Rank: Member

      Can't figure this one out.

      Goal, Read the value of the scheduled task, the one that sync groups policy and project at what time the task will run every day by reading the task triggers.
      $TaskGpUpdateComp = get-scheduledtask -Taskpath "\Microsoft\Windows\GroupPolicy\" -TaskName "{3E0A038B-D834-4930-9981-E89C9BFF83AA}" | select -ExpandProperty Triggers

      $StartTime = [DateTime]::Parse($TaskGpUpdateComp.StartBoundary)

      My problem is I can't figure out how to take the Repetition.Interval value which is in this format :  https://docs.microsoft.com/en-us/windows/win32/taskschd/taskschedulerschema-interval-repetitiontype-element and

      PS C:\windows\system32> $TaskGpUpdateComp.Repetition.Interval
      PT1H46M

      convert back in a powershell time-span object.

      I have tried :
      [System.TimeSpan]::Parse($TaskGpUpdateComp.Repetition.Interval)

      [System.TimeSpan]::ParseExact("$TaskGpUpdateComp.Repetition.Interval", no idea what parse string would look like )

      Other option could be extra hour, minutes from the string with a regex match ?

      Any tips ?

       

       

    • #191611
      Participant
      Topics: 10
      Replies: 1284
      Points: 1,084
      Helping Hand
      Rank: Community Hero

      This forum has a function to parse the interval:

      Help with a powershell script to pull Scheduled Tasks from PowerShell

    • #191638
      Participant
      Topics: 3
      Replies: 5
      Points: 55
      Rank: Member

      ok thanks I had a look at link and decide it was too complex for me so did something at my skill level used a regex match  here is final code,  its reads the 1st run time for GPO Scheduled task then project into the future at what time the task should run.

      $TaskGpUpdateComp = get-scheduledtask -Taskpath "\Microsoft\Windows\GroupPolicy\" -TaskName "{3E0A038B-D834-4930-9981-E89C9BFF83AA}" | select -ExpandProperty Triggers

      # 1st event the GPO ran at
      $StartTime = [DateTime]::Parse($TaskGpUpdateComp.StartBoundary)

      # Convert Repetition Interval to timespan
      ($TaskGpUpdateComp.Repetition.Interval | out-string) -match 'PT(?\d)H(?\d\d)M'
      if ($matches) { $TimeSpan = New-TimeSpan -Hours $Matches.hour -Minutes $Matches.minutes}

      #Project the time at which GPO update computer will run.
      $count = 0
      do {
      if ($count -eq 0) {$nextProcessing = $StartTime; write-host "GPO processing at $nextProcessing" }

      $nextProcessing += ($TimeSpan)
      write-host "GPO processing at $nextProcessing"
      $count++
      }

      until ( $nextProcessing -ge $(get-date) )

      the output from my PC
      GPO processing at 12/02/2019 07:48:52
      GPO processing at 12/02/2019 09:34:52
      GPO processing at 12/02/2019 11:20:52
      GPO processing at 12/02/2019 13:06:52
      GPO processing at 12/02/2019 14:52:52
      GPO processing at 12/02/2019 16:38:52
      GPO processing at 12/02/2019 18:24:52
      GPO processing at 12/02/2019 20:10:52
      GPO processing at 12/02/2019 21:56:52
      GPO processing at 12/02/2019 23:42:52
      GPO processing at 12/03/2019 01:28:52
      GPO processing at 12/03/2019 03:14:52
      GPO processing at 12/03/2019 05:00:52
      GPO processing at 12/03/2019 06:46:52
      GPO processing at 12/03/2019 08:32:52
      GPO processing at 12/03/2019 10:18:52
      GPO processing at 12/03/2019 12:04:52
      GPO processing at 12/03/2019 13:50:52
      GPO processing at 12/03/2019 15:36:52
      GPO processing at 12/03/2019 17:22:52
      GPO processing at 12/03/2019 19:08:52
      GPO processing at 12/03/2019 20:54:52
      GPO processing at 12/03/2019 22:40:52
      GPO processing at 12/04/2019 00:26:52
      GPO processing at 12/04/2019 02:12:52
      GPO processing at 12/04/2019 03:58:52
      GPO processing at 12/04/2019 05:44:52
      GPO processing at 12/04/2019 07:30:52
      GPO processing at 12/04/2019 09:16:52
      GPO processing at 12/04/2019 11:02:52
      GPO processing at 12/04/2019 12:48:52

      which matches the GPO task history !

Viewing 2 reply threads
  • You must be logged in to reply to this topic.